Apache Openoffice安装及使用Java进行word转pdf总结


前言

项目中需要使用在线预览Word的功能,可借助Apache OpenOffice服务然先将word转换成pdf,然后在通过在线pdf在线预览的迂回方式实现,以下记录此过程。


一、安装并启动OpenOffice的服务(RPM安装方式)(需要使用root用户安装启动)

1.安装OpenOffice,启动服务

去官网找下载包,官网地址:https://www.openoffice.org/download/
我下载的是4.1.10这个,下载地址:https://udomain.dl.sourceforge.net/project/openofficeorg.mirror/4.1.10/binaries/zh-CN/Apache_OpenOffice_4.1.10_Linux_x86-64_install-rpm_zh-CN.tar.gz
下载速度特别慢,因此我将下载的文件已上传在csdn资源中
https://download.csdn.net/download/jxlhljh/18879159

先参考着官网安装文档来进行,官网安装文档地址:https://www.openoffice.org/download/common/instructions.html#linux-rpm
在这里插入图片描述

##解压
mkdir /root/openoffice
tar -zxvf /root/Apache_OpenOffice_4.1.10_Linux_x86-64_install-rpm_zh-CN.tar.gz -C /root/openoffice
cd /root/openoffice/zh-CN/RPMS
yum localinstall *.rpm
cd /root/openoffice/zh-CN/RPMS/desktop-integration
yum localinstall openoffice4.1.10-redhat-menus-4.1.10-9807.noarch.rpm

##安装成功后,会在 /opt目录下生成openoffice4文件夹, 即/opt/openoffice4

##添加字体库(ps:不然会出现转换中文字体乱码或直接不显示中文字体)
在 /usr/share/fonts 目录下新建文件夹,windowfonts,如图
在这里插入图片描述
##如果上面的步骤中发现找不到/usr/share/fonts目录说明部分依赖没安装,增加以下依赖安装

yum install libXext.x86_64
yum groupinstall "X Window System"

然后将我们windows系统得字体库文件拷贝到windowfonts下,windows字体库路径如下图
在这里插入图片描述
##启动服务,启动命令为:

/opt/openoffice4/program/soffice.bin -headless -accept="socket,host=0.0.0.0,port=8100;urp;" -nofirststartwizard & 

##查看进程
ps -ef|grep openoffice4

##或者
netstat -luntp|grep 8100

至此,安装并启动OpenOffice的服务完成,接下来进行Java调用测试

二、Java使用Openoffice进行word转Pdf,并进行预览试验。

1.进行单元测试

编写OpenOfficeTransferTest单元测试类

package openofficetest;

import java.io.File;

import org.junit.Test;
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.StreamOpenOfficeDocumentConverter;

public class OpenOfficeTransferTest {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	private String connectIp = "192.168.56.101";
	private int connectPort = 8100;

	// word转pdf,使用openoffice进行转换
	@Test
	public void wordToPdfTest() {

		OpenOfficeConnection connection = null;
		logger.info("connecting start...");
		try {
			
			connection = new SocketOpenOfficeConnection(connectIp, connectPort);
			connection.connect();

			logger.info("connected...> " +connection);
			DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);

			logger.info("convert starting.....");
			long startMs = System.currentTimeMillis();
			converter.convert(new File("D:\\temp\\1.docx"), new File("D:\\temp\\1.pdf"));
			long endMs = System.currentTimeMillis();
			logger.info("convert success, spend time is : {} ms.",(endMs-startMs) );

		} catch (Exception e) {
			logger.error("", e);
		} finally {

			if(connection != null) {
				connection.disconnect();
				logger.info("connection disconnected. ");
			}
		}
	}

}

##以上单元测试的jar包依赖如下,(有部分是直接依赖本地的jar,因为maven下载不到)

	<dependencies>

		<!-- junit start -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.13</version>
		</dependency>
		<!-- junit end -->

		<!-- office to pdf need install something start -->
		<dependency>
			<groupId>com.artofsolving</groupId>
			<artifactId>jodconverter</artifactId>
			<version>2.2.2</version>
			<scope>system</scope>
			<systemPath>${basedir}/lib/jodconverter-2.2.2.jar</systemPath>
		</dependency>
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>ridl</artifactId>
			<version>4.1.2</version>
			<scope>system</scope>
			<systemPath>${basedir}/lib/ridl-4.1.2.jar</systemPath>
		</dependency>
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>juh</artifactId>
			<version>4.1.2</version>
			<scope>system</scope>
			<systemPath>${basedir}/lib/juh-4.1.2.jar</systemPath>
		</dependency>
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>jurt</artifactId>
			<version>4.1.2</version>
			<scope>system</scope>
			<systemPath>${basedir}/lib/jurt-4.1.2.jar</systemPath>
		</dependency>
		<dependency>
			<groupId>org.openoffice</groupId>
			<artifactId>unoil</artifactId>
			<version>4.1.2</version>
			<scope>system</scope>
			<systemPath>${basedir}/lib/unoil-4.1.2.jar</systemPath>
		</dependency>
		<!-- office to pdf need install something end-->

		<!-- commons-io start -->
		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.8.0</version>
		</dependency>
		<!-- commons-io end -->

		<!-- jackson start -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.12.1</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-core</artifactId>
			<version>2.12.1</version>
		</dependency>

		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-annotations</artifactId>
			<version>2.12.1</version>
		</dependency>
		<!-- jackson end -->

		<!-- log start -->
		<dependency>
			<groupId>org.slf4j</groupId>
			<artifactId>slf4j-api</artifactId>
			<version>1.7.25</version>
		</dependency>

		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-core</artifactId>
			<version>1.2.3</version>
		</dependency>
		<dependency>
			<groupId>ch.qos.logback</groupId>
			<artifactId>logback-classic</artifactId>
			<version>1.2.3</version>
		</dependency>
		<!-- log end -->

	</dependencies>

##不能下载到的jar如下,可直接在maven上搜索然后手工下载下来。
在这里插入图片描述

2.测试效果

在这里插入图片描述
转换后:
在这里插入图片描述

3.测试代码地址

测试代码地址:https://gitee.com/jxlhljh/openofficetest.git

三、Wordl转Pdf的restful服务提供(待完成)。

以上的单元测试由于每次都需要重新连接,占用资源,耗时太久,接下来再完善一个springboot的http服务工程,用来提供转换服务,并提供在线预览的功能(待完成)

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值