使用jodconverter转换文档为PDF

使用docconverter和openoffice服务完成将文档转换为pdf文件,跨平台,支持97-03、07及以后的文档


1.下载openoffice

有Windows、Linux等版本,下载后安装

下载地址:http://www.openoffice.org/download/index.html

2.jodconverter Maven依赖

(1).2.2.1版本

中央仓库里面有2.2.1和3.0-alfresco-patched-20141024版本,后者无法下载;
使用2.2.1版本可以正常转换txt、97-03的doc、xls、ppt为PPT,07及以后则会提示:unknown document format for file(未知文档格式)

<dependency>
	<groupId>com.artofsolving</groupId>
    	<artifactId>jodconverter</artifactId>
    	<version>2.2.1</version>
</dependency>

(2).2.2.2版本

中央仓库没发现,去官网down了一个;之后需要将所依赖的jar包手动添加至到本地maven中(这个大家都会的就不多说了);

解压后进入lib目录,有个DEPENDENCIES.txt文件,这里面说明了所依赖的jar包,内容如下(大家都看得懂就不翻译了):

To use the library in your own Java app you need

  •  * commons-io
  •  * jodconverter
  •  * juh
  •  * jurt
  •  * ridl
  •  * slf4j-api
  •  * slf4j-jdk14 or another slf4j implementation - see http://slf4j.org
  •  * unoil
  •  * xstream - only if you use XmlDocumentFormatRegistry
The command line interface additionally requires
  •  * commons-cli
  •  * jodconverter-cli

相关maven依赖如下,日志、io自行添加:

<!-- 需手动添加至仓库 -->

<dependency>
	<groupId>com.artofsolving</groupId>
	<artifactId>jodconverter</artifactId>
	<version>2.2.2</version>
</dependency>
<!-- openoffice提供的jar,直接引用 -->
<dependency>
	<groupId>org.openoffice</groupId>
	<artifactId>juh</artifactId>
	<version>3.0.1</version>
</dependency>
<dependency>
	<groupId>org.openoffice</groupId>
	<artifactId>jurt</artifactId>
	<version>3.0.1</version>
</dependency>
<dependency>
	<groupId>org.openoffice</groupId>
	<artifactId>ridl</artifactId>
	<version>3.0.1</version>
</dependency>
<dependency>
	<groupId>org.openoffice</groupId>
	<artifactId>unoil</artifactId>
	<version>3.0.1</version>
</dependency>

下载地址:https://sourceforge.net/projects/jodconverter/files/

文档转pdf

方式一:

1.手动启动openoffice服务,在程序中调用该服务转换,本地openoffice安装在G:/Program Files (x86)/OpenOffice 4目录下

cd G:\Program Files (x86)\OpenOffice 4\program
soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
2.代码

运行该程序时需先启动openoffice服务;测试了txt、doc、docx、xls、xlsx、ppt格式文件,均能转换成功

/**
 * 文档2pdf

 * @param docFile 文件路径
 * @param pdfFile pdf输出路径
 * @return
 */
public static boolean doc2pdf(File docFile, File pdfFile) {
	boolean result = false;// 转换结果
	if (docFile.exists()) {
		if (!pdfFile.exists()) {
			OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
			try {
				connection.connect();
				DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
				converter.convert(docFile, pdfFile);
				// 关闭连接
				connection.disconnect();
				result = true;
					
				LOG.info("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");
			} catch (java.net.ConnectException e) {
				LOG.error("openoffice服务未启动", e);
			} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
				LOG.error("读取转换文件失败", e);
			} catch (Exception e) {
				LOG.error("转换失败", e);
			}
		} else {
			result = true;
			LOG.info("****已经转换为pdf,不需要再进行转化****");
		}
	} else {
		LOG.info("****需要转换的文档不存在,无法转换****");
	}
	return result;
}


方式二:

程序中启动openoffice服务,并转换文档为pdf;测试文件同方式一

/**
 * doc转pdf(程序启动openoffice)
 * 
 * @param inputFile 输入文件
 * @param outputFile 输出文件
 * @return
 */
public static boolean doc2pdf2(File inputFile, File outputFile) {
	boolean result = false;
	// OpenOffice的安装目录
	String OpenOffice_HOME = "G:/Program Files (x86)/OpenOffice 4";
	if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {
		OpenOffice_HOME += "/";
	}
	Process process = null;
	try {
		// 启动OpenOffice的服务
		String command = OpenOffice_HOME
				+ "program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\"";
		process = Runtime.getRuntime().exec(command);
		// 连接 OpenOffice实例,运行在8100端口
		OpenOfficeConnection connection = new SocketOpenOfficeConnection("127.0.0.1", 8100);
		connection.connect();

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

		// 关闭连接
		connection.disconnect();
		// 销毁OpenOffice服务的进程
		process.destroy();

		LOG.info("****pdf转换成功,PDF输出:" + outputFile.getPath() + "****");
		return true;
	} catch (Exception e) {
		LOG.error("pdf转换失败", e);
	} finally {
		if (process != null) {
			process.destroy();
		}
	}
	return result;
}


参考博客:http://huangronaldo.iteye.com/blog/1628339






猜你喜欢

转载自blog.csdn.net/mytt_10566/article/details/79519736
今日推荐