1、下载官网
OpenOffice 下载地址 Apache OpenOffice – Official Download
2、安装启动服务
window环境:
进入openoffice安装目录:/program/
开启服务:cmd命令
soffice -headless -accept=“socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard
Linux环境:
1. 解压缩
tar -xvzf ***.tar.gz
2.安装openoffice
cd ***/
cd RPMS/
rpm -ivh –force –nodeps *.rpm
方式一样进入安装目录启动服务
一般Linux在/opt/openoffice4下
以后台服务方式启动openoffice
/opt/openoffice4/program/soffice -headless -accept=”socket,host=127.0.0.1,port=8100;urp;” -nofirststartwizard &
3、整合Springboot
3.1 pom文件
注:jodconverter 2.2.2版本以下不支持docx等文件类型装换
JodConverter 下载地址JODConverter – Browse /JODConverter at SourceForge.net
<dependency>
<groupId>com.artofsolving</groupId>
<artifactId>jodconverter</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>jurt</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>ridl</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>juh</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>org.openoffice</groupId>
<artifactId>unoil</artifactId>
<version>4.1.2</version>
</dependency>
3.2、yml配置文件
-
openOffice:
-
host: 127.0.0.1
-
port: 8100
3.3OpenOfficeUtil工具类
/**
* @Description: office转换pdf工具类
* @author: djq
*/
@Component
@Slf4j
public class OpenOfficeUtil {
/**
* @Description: office转换pdf
* @author: djq
* @param file
* @param outFile
* @return
*/
public void office2Pdf(File file, File outFile) {
// 判断源文件是否存在
if (!file.exists()) {
log.error(“转换源文件不存在!”);
throw new RuntimeException(“转换源文件不存在!”);
}
// 创建连接
OpenOfficeConnection connection = null;
try {
// 远程连接OpenOffice服务
log.info(“远程连接OpenOffice服务”);
connection = new SocketOpenOfficeConnection(host, port);
connection.connect();
// 创建文件转换器
DocumentConverter converter = new StreamOpenOfficeDocumentConverter(connection);
// 开始转换
converter.convert(file, outFile);
if (outFile.exists()) {
log.info(“文件转换成功”);
} else {
log.info(文件转换失败”);
}
} catch (ConnectException e) {
e.printStackTrace();
log.error(“OpenOffice服务启动失败”;
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
@Value(“${openOffice.host}”)
private String host;
@Value(“${openOffice.port}”)
private int port;
}3.4测试文件 日志
写个测试类自动测试,启动测试成功
package com.zgdz.util.controller;
import java.io.File;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import com.zgdz.util.util.OpenOfficeUtil;
import lombok.extern.slf4j.Slf4j;
/**
* @Description: test
* @author: djq
* @date: 2022-04-13 19:03
*/
@RestController
@Slf4j
public class OpenOfficeController {
@Autowired
private OpenOfficeUtil openOfficeUtil;@RequestMapping(value = “/test”)
public Object testOpenOffice() {
String wordpath = “D:\\openOffice测试.docx”;
String pdfpath = “D:\\openOffice测试.pdf”;
openOfficeUtil.office2Pdf(new File(wordpath), new File(pdfpath));
return true;
}
}2022-04-13 19:02:38.728 INFO 13460 — [nio-9527-exec-1] com.zgdz.util.util.OpenOfficeUtil : 远程连接OpenOffice服务
2022-04-13 19:02:38.787 INFO 13460 — [nio-9527-exec-1] c.a.j.o.c.SocketOpenOfficeConnection : connected
2022-04-13 19:02:39.538 INFO 13460 — [nio-9527-exec-1] com.zgdz.util.util.OpenOfficeUtil :文件转换成功
2022-04-13 19:02:39.539 INFO 13460 — [nio-9527-exec-1] c.a.j.o.c.SocketOpenOfficeConnection : disconnected转自:https://blog.csdn.net/dujianqiu/article/details/124154051