JodConverter doc转pdf (Debian 8)

环境信息

虚拟机节点(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

转载于:https://my.oschina.net/norxiva/blog/711900

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值