一、 背景
因为项目中需要使用word转pdf功能,因为转换速度原因,最后选用了libreoffice,原因及部署请参考
linux ubuntu环境安装libreoffice,word转pdf
远程调用的话可选docker部署,请看2.3.1
二、springboot整合libreoffice
其实springboot整合libreoffice有两种方式,一种是使用本地的libreoffice,一种是使用远程服务的libreoffice(这个好多文章中没有提到,也是自己踩的坑算是)
2.1、整合本地服务
引入pom
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.4.2</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.4.2</version>
</dependency>
yml配置
jodconverter:
local:
enabled: true
#window地址: D:\workplaces\jcxx\libreoffice 请自行补全
#linux地址: /opt/libreoffice24.2
office-home: /opt/libreoffice24.2
# 端口(线程)
portNumbers: [8101,8102,8103]
maxTasksPerProcess: 100
# 任务执行的超时时间
task-execution-timeout: 360000
# 任务队列的超时时间
task-queue-timeout: 360000
# 一个进程的超时时间
process-timeout: 360000
使用也很简单
@Resource
private DocumentConverter documentConverter;
public void test() {
//流转换
documentConverter.convert(inputStream).as(DefaultDocumentFormatRegistry.DOCX).to(outStream).as(DefaultDocumentFormatRegistry.PDF).execute();
//文件转换,sourceFile和targetFile都是File类实例
documentConverter.convert(sourceFile).to(targetFile).as(DefaultDocumentFormatRegistry.PDF).execute();
}
2.2、整合远程服务
说一下怎么发现的,学过springboot的应该都知道,整合其他服务时候应该都有个配置类xxxAutoConfiguration
于是发现了除了一个local外,还有个remote,才发现可以直接调用远程服务,发现了那就可以整合使用,如下
pom引入
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.4.2</version>
</dependency>
<dependency>
<groupId>org.jodconve