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
    评论
要在Linux环境中使用Java JODConverter批量换文件为PDF,你可以按照以下步骤进行操作: 1. 安装OpenOffice或LibreOffice:JODConverter依赖于OpenOffice或LibreOffice,因此需要先在Linux环境中安装其中一个软件。 2. 安装JODConverter:下载JODConverter的压缩包并解压缩,然后将JAR文件添加到你的Java类路径中。 3. 编写Java代码:编写Java代码来实现批量换文件为PDF。以下是一个示例代码: ```java import java.io.File; import java.util.HashMap; import java.util.Map; import org.artofsolving.jodconverter.OfficeDocumentConverter; import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration; import org.artofsolving.jodconverter.office.OfficeManager; public class ConvertFilesToPDF { public static void main(String[] args) { // Set up the OfficeManager configuration DefaultOfficeManagerConfiguration config = new DefaultOfficeManagerConfiguration(); config.setOfficeHome("/usr/lib/libreoffice"); config.setPortNumber(8100); // Start the OfficeManager OfficeManager manager = config.buildOfficeManager(); manager.start(); // Set up the converter OfficeDocumentConverter converter = new OfficeDocumentConverter(manager); // Set up the input and output directories File inputDir = new File("/path/to/input/dir"); File outputDir = new File("/path/to/output/dir"); // Get a list of all files in the input directory File[] files = inputDir.listFiles(); // Loop through all files and convert them to PDF for (File file : files) { String inputFilePath = file.getAbsolutePath(); String outputFilePath = outputDir.getAbsolutePath() + "/" + file.getName() + ".pdf"; Map<String, Boolean> filterData = new HashMap<String, Boolean>(); filterData.put("Hidden", true); converter.convert(new File(inputFilePath), new File(outputFilePath), filterData); } // Stop the OfficeManager manager.stop(); } } ``` 4. 运行代码:使用Java命令运行代码,即可批量换文件为PDF,输出文件将保存在指定的输出目录中。 以上是在Linux环境中使用Java JODConverter批量换文件为PDF的步骤,希望对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值