linux上使用libreoffice转换word成pdf
1, linux安装libreoffice
1.1, 先安装依赖
yum install cairo -y
yum install cups-libs -y
yum install libSM -y
2, 安装好依赖之后,去libreoffice官网下载安装包到本地,官网地址:https://www.libreoffice.org/download/download/
3,安装包下载完成后,上传到linux服务器,/usr/local/libreoffice, 地址可以随意放。
4,解压安装
4.1, 解压:
tar -xvf LibreOffice_7.0.2_Linux_x86-64_rpm.tar.gz
4.2, 进入解压目录 :
cd LibreOffice_7.0.2_Linux_x86-64_rpm/RPMS
4.3 安装:
sudo yum localinstall *.rpm
5,安装完成后,查看安装路径
which libreoffice7 看到路径为 /usr/bin/libreoffice7
ll /usr/bin/libreoffice7得到具体地址.
6, 配置环境变量
vim /etc/profile
加入环境变量:
export LibreOffice_PATH=/opt/libreoffice7/program
export PATH=
L
i
b
r
e
O
f
f
i
c
e
P
A
T
H
:
LibreOffice_PATH:
LibreOfficePATH:PATH
保存: source /etc/profile
7,安装好libreoffice后,开始编码
7.1 引入依赖包
org.jodconverter
jodconverter-core
4.2.2
org.jodconverter
jodconverter-local
4.2.2
org.jodconverter
jodconverter-spring-boot-starter
4.2.2
org.libreoffice
ridl
5.4.2
7.2 转换word to pdf
public class PdfconvertService {
/*@Autowired
private DocumentConverter documentConverter;
public void docToPdf() {
File inputFile = new File("D:\\template\\test_032521.docx");
File outputFile = new File("D:\\template\\test_032521.pdf");
try {
InputStream inputStream = new FileInputStream(inputFile);
OutputStream outputStream = new FileOutputStream(outputFile);
documentConverter
.convert(inputStream)
.as(DefaultDocumentFormatRegistry.DOCX)
.to(outputStream)
.as(DefaultDocumentFormatRegistry.PDF)
.execute();
inputStream.close();
outputStream.close();
} catch (OfficeException e) {
System.out.println("convert pdf fail.");
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
8, linux上转换pdf可能会出现字体不一致问题,可安装windows字体到linux,如何在linux安装字体可以看我的另一篇博客。