Demo地址
https://download.csdn.net/download/L_C_Baker/12316480
windows安装
-
下载安装包进行安装
https://zh-cn.libreoffice.org -
设置环境变量(可选)
增加path,LibreOffice的安装目录。
C:\Program Files\LibreOffice\program
-
LibreOffice装换命令尝试(注意输入输出文件地址)
# windows soffice.exe --headless --invisible --convert-to pdf e:\tmp\123.docx --outdir e:\tmp # linux /usr/bin/libreoffice6.0 --headless --invisible --convert-to pdf /tmp/123.docx --outdir /tmp
-
增加pom依赖
<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>
注意: 在这里说明特别一下,jodconverter自4.2开始,对LibreOffice相关功能从jodconverter-core中分离出来,封装到为jodconverter-local,另外新增了jodconverter-online,支持LibreOffice online server的远程调用。
-
配置 application.properties(windows配置未生效?)
jodconverter.local.enabled=true # 设置LibreOffice主目录 jodconverter.local.office-home=${pom.office.home} # 开启多个LibreOffice进程,每个端口对应一个进程 jodconverter.local.portNumbers=8100,8101,8102 # LibreOffice进程重启前的最大进程数 jodconverter.local.maxTasksPerProcess=100
${pom.office.home}没有生效,暂时不知道是什么原因。
-
使用Maven的多环境配置(windows配置未生效?)
<profiles> <profile> <!-- windows环境 --> <id>win</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <pom.office.home>C:/Program Files/LibreOffice</pom.office.home> </properties> </profile> <profile> <!-- linux环境 --> <id>linux</id> <properties> <pom.office.home>/opt/libreoffice6.0</pom.office.home> </properties> </profile> </profiles>
-
调用
@RequestMapping(value = “/convertfiles”, method = RequestMethod.GET)
public String convertFiles() {
String re = “convert files fail”;File wfile = new File("F:/testfiles/test.docx"); File pfile = new File("F:/testfiles/pfile.pdf"); try { documentConverter.convert(wfile).to(pfile).execute(); re = "convert files ok"; } catch (OfficeException e) { e.printStackTrace(); } return re; }
Linux安装
- 下载libreoffice安装包、语言包
cd /opt
#下载
wget http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.1.2/rpm/x86_64/LibreOffice_6.1.2_Linux_x86-64_rpm.tar.gz)
wget http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.1.2/rpm/x86_64/LibreOffice_6.1.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
# 解压
tar xzvf LibreOffice_6.4.2.2_Linux_x86-64_rpm.tar.gz
tar xzvf LibreOffice_6.4.2.2_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
- 安装
yum install /opt/LibreOffice_6.4.2.2_Linux_x86-64_rpm/RPMS/*.rpm
yum install /opt/LibreOffice_6.4.2.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm
- 文件转换测试
修改源文件地址和输出地址,转换成功即表示ok。
/usr/bin/libreoffice6.4 --headless --invisible --convert-to pdf /tmp/sample.docx --outdir /tmp