第一步 安装Libreoffice
在线安装:
Ubuntu
sudo apt update
sudo apt install libreoffice
Centos
sudo yum update
sudo yum install libreoffice
如果安装失败记得更新数据源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sudo yum clean all
sudo yum makecache
sudo yum update
离线安装:
下载地址:LibreOffice 官网
cd /opt
解压文字编码和libreoffice
tar -zxvf LibreOffice_7.5.9.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
tar -zxvf LibreOffice_7.5.9.2_Linux_x86-64_rpm.tar.gz
安装LibreOffice
cd LibreOffice_7.5.9.2_Linux_x86-64_rpm/RPMS/
yum localinstall *.rpm
cd LibreOffice_7.5.9.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/
安装字体编码
yum localinstall *.rpm
验证是否安装成功
libreoffice7.5 --version
卸载LibreOffice
yum remove libreoffice*
更新依赖
yum install -y libreoffice-headless
报错更新依赖
yum install cairo -y
yum install cups-libs -y
yum install libSM -y
第二步 maven依赖
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-core</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.2.0</version>
</dependency>
<dependency>
<groupId>org.libreoffice</groupId>
<artifactId>ridl</artifactId>
<version>5.4.2</version>
</dependency>
第三步 配置DocumentConverter
jodconverter:
local:
enabled: true
# 设置LibreOffice主目录
# office-home: /opt/libreoffice6.4
office-home: C:/Program Files/LibreOffice
# 开启多个LibreOffice进程,每个端口对应一个进程
portNumbers: 9080,9081,9089
# LibreOffice进程重启前的最大进程数
maxTasksPerProcess: 100
第四步 使用DocumentConverter
使用@Autowire注入 documentConverter
@Autowired
private DocumentConverter documentConverter;
#使用流的方式转换PDF
documentConverter.convert(inputStream)
.as(fileExtension.compareToIgnoreCase(DefaultDocumentFormatRegistry.DOC.getExtension()) == 0 ? DefaultDocumentFormatRegistry.DOC : DefaultDocumentFormatRegistry.DOCX)
.to(outputStream)
.as(DefaultDocumentFormatRegistry.PDF)
.execute();
#使用文件方式转换成PDF
documentConverter
.convert(oldFile)
.to(file)
.execute();