OpenOffice实现word转pdf

3 篇文章 0 订阅
2 篇文章 0 订阅

OpenOffice

<dependency>
			<groupId>org.apache.directory.studio</groupId>
			<artifactId>org.apache.commons.io</artifactId>
			<version>2.4</version>
		</dependency>
<dependency>
			<groupId>com.artofsolving</groupId>
			<artifactId>jodconverter</artifactId>
			<version>2.2.2</version>
		</dependency>
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>juh</artifactId>
			<version>3.1.0</version>
		</dependency>

		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>unoil</artifactId>
			<version>3.0.0</version>
		</dependency>

首先在路径D:\tools\OpenOffice 4\program下执行

soffice.exe -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
import java.io.File;
import java.io.IOException;

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 DOC2PDFUtil {



	//使用openoffice 将word格式的文件转换为pdf格式
	public static void word2Pdf(String srcPath, String desPath) throws IOException {
		// 源文件目录
		File inputFile = new File(srcPath);
		if (!inputFile.exists()) {
			System.out.println("源文件不存在!");
			return;
		}
		// 输出文件目录
		File outputFile = new File(desPath);
		if (!outputFile.getParentFile().exists()) {
			outputFile.getParentFile().exists();
		}
		// 调用openoffice服务线程
		//String command = "D:\\tools\\OpenOffice 4\\program\\soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100\"";
		//Process p = Runtime.getRuntime().exec(command);

		// 连接openoffice服务
		OpenOfficeConnection connection = new SocketOpenOfficeConnection(
				"127.0.0.1", 8100);
		connection.connect();

		// 转换word到pdf
		DocumentConverter converter = new OpenOfficeDocumentConverter(
				connection);
		converter.convert(inputFile, outputFile);

		// 关闭连接
		connection.disconnect();

		// 关闭进程
		//p.destroy();
		System.out.println("转换完成!");
	}

	/**
	 * 测试main方法
	 * @param args
	 */
	public static void main(String[] args) throws IOException {
		word2Pdf("C:\\Users\\Administrator\\Desktop\\tianjin\\yangli\\周报20190322.doc","C:\\Users\\Administrator\\Desktop\\tianjin\\yangli\\周报20190322.pdf");

	}
}

使用jodconverter 2.2.2,不要使用2.2.1 因为不支持docx的转换
报错

Caused by: com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException: conversion failed: could not load input document

解决方法:

//之前使用这种方式
//DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
//改成下面这种方式之后问题解决
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);

命令行转

soffice --headless --convert-to 目标格式(如pdf) 转格式文件 --outdir 目标文件夹
./soffice --headless --convert-to pdf /home/xx.docx --outdir /home/xx.pdf
/opt/openoffice4/program/soffice --headless --convert-to pdf /home/xx.docx --outdir /home/xx.pdf
soffice.exe --headless --convert-to pdf E:\temp\xx.docx --outdir E:\temp\xx.pdf

参考文章:https://blog.csdn.net/zgzdlm/article/details/84923457

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值