环境信息
虚拟机节点(192.168.100.171<debian171>)
Debian jessie 8.5
Jodconverter 2.2.1
安装libreoffice
#apt安装
sudo apt-get install libreoffice
sudo soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &
#deb安装
tar xvf LibreOffice_5.1.4_Linux_x86-64_deb.tar.gz
cd LibreOffice_5.1.4.2_Linux_x86-64_deb/DEBS/
dpkg -i *.deb
cd /opt/libreoffice5.1/program/
soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &
如果报“error while loading shared libraries: libGL.so.1: cannot open shared object file: No such file or directory”,需要安装下面的软件包
sudo apt-get install libgl1-mesa-glx
如果报“error while loading shared libraries: libSM.so.6: cannot open shared object file”,需要安装下面的软件包
sudo apt-get install libsm6
参考:http://libre-software.net/how-to-install-libreoffice-on-ubuntu-linux-mint/
java代码
package surfin.example.jodconverter.simpledemo;
import java.io.File;
import java.net.ConnectException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;
public class BootStrap {
private static final Logger log = LoggerFactory.getLogger(BootStrap.class);
public static void main(String[] args) {
if(args.length < 2){
log.info("usage: java -jar surfin-example-jodconverter.jar (source_file) (target_file)");
return;
}
OpenOfficeConnection connection = null;
DocumentConverter converter = null;
try {
connection = new SocketOpenOfficeConnection(8100);
connection.connect();
converter = new OpenOfficeDocumentConverter(connection);
File inputFile = new File(args[0]);
File outputFile = new File(args[1]);
// convert
converter.convert(inputFile, outputFile);
} catch (ConnectException e) {
log.error(e.getMessage(), e);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}
中文问题
#复制字体后重启libreoffice
sudo cp simhei.ttf /usr/share/fonts/truetype/openoffice
sudo cp simsun.ttc /usr/share/fonts/truetype/openoffice
注意:存放字体到/usr/lib/libreoffice/share/fonts/truetype和/usr/local/java/jdk1.8.0_92/jre/lib/fonts都没有生效
小结
jodconverter的转换效果不是太尽人意,字体样式会有错位,在window上除字体存在问题外,格式还行,应该与libreoffice和msoffice样式区分有关
其他类似框架:docx4j,xdocreport
docx4j测试代码
public static void main(String[] args) {
long start = System.currentTimeMillis();
// 1) Load DOCX into WordprocessingMLPackage
InputStream is;
try {
is = new FileInputStream(new File("d:/test1.docx"));
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(is);
// 2) Prepare Pdf settings
PdfSettings pdfSettings = new PdfSettings();
// 3) Convert WordprocessingMLPackage to Pdf
OutputStream out = new FileOutputStream(new File("d:/test-docx4j.pdf"));
PdfConversion converter = new org.docx4j.convert.out.pdf.viaXSLFO.Conversion(wordMLPackage);
converter.output(out, pdfSettings);
log.info("Generate pdf/HelloWorld.pdf with " + (System.currentTimeMillis() - start) + "ms");
} catch (FileNotFoundException e) {
log.error(e.getMessage(),e );
} catch (Docx4JException e) {
log.error(e.getMessage(),e );
}
}
xdocreport代码
long start = System.currentTimeMillis();
// 1) Load DOCX into XWPFDocument
InputStream is;
try {
is = new FileInputStream(new File("d:/test1.docx"));
XWPFDocument document = new XWPFDocument(is);
// 2) Prepare Pdf options
PdfOptions options = PdfOptions.create();
// 3) Convert XWPFDocument to Pdf
OutputStream out = new FileOutputStream(new File("d:/test1-xdoc.pdf"));
PdfConverter.getInstance().convert(document, out, options);
log.info("Generate pdf/HelloWorld.pdf with " + (System.currentTimeMillis() - start) + "ms");
} catch (FileNotFoundException e) {
log.error(e.getMessage(), e);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
小结
docx4j和xdocreport转换效果还不如jodconverter,样式和内容都会有缺失
参考资料
http://m.myexception.cn/powerdesigner/414230.html
http://wenku.baidu.com/view/79ad1bd4360cba1aa811dadb.html
http://it.chinawin.net/os/article-1d426.html
http://www.it610.com/article/536230.htm
http://slikel.iteye.com/blog/1677579/
http://thexfile0515.blogspot.com/2012/04/jodconverter-microsoft-office-pdf.html
http://5iqiong.blog.51cto.com/2999926/1188961