1. 依赖项
1.1. 系统依赖
1. Java8
2. LibreOffice6.2+
1.2. 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>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>pdfbox</artifactId>
<version>2.0.9</version>
</dependency>
<dependency>
<groupId>org.apache.pdfbox</groupId>
<artifactId>fontbox</artifactId>
<version>2.0.9</version>
</dependency>
2. 下载/安装
2.1. LibreOffice 下载
1.https://zh-cn.libreoffice.org/download/download/?type=deb-x86_64&version=6.2.8&lang=zh-CN
2.根据 步骤1选择对应系统下载对应的安装包 ,然后点击步骤2 所指连接 下载对应系统语言包。
2.2. libreOffice 安装
2.2.1. Windows 安装
点 击 下 载 的 文 件 开 始 安 装
选择安装目录 注 :不要在中文目录下 并记住安装目录 后面配置的时候需要使用
2.2.2. Linux 安装(ubuntu16+)
- 上 传 对 应 系 统 的 压 缩 包 到 系 统 指 定 目 录
- 解压对应压缩包
- tar -xvf LibreOffice_6.2.8_Linux_x86-64_deb.tar.gz
- tar -xvf LibreOffice_6.2.8_Linux_x86-64_deb_langpack_zh-CN.tar.gz
- 解压后文件
- cd LibreOffice_6.2.8.2_Linux_x86-64_deb
- 进入DEBS 文件夹 cd DEBS/ 执行命令 sudo dpkg -i *.deb
- 回退带解压目录 执行 cd LibreOffice_6.2.8.2_Linux_x86-64_deb_langpack_zh-CN/
- 进入DEBS 文件夹 cd DEBS/ 执行命令 sudo dpkg -i *.deb
2.2.3. cd /opt/libreoffice6.2/ 查看 libreOffice 安装目录(默认安装目录)
2.2.4. LibreOffice 手动启动命令:
/opt/libreoffice6.2/program/soffice.bin --headless --accept=socket,host=127.0.0.1,port=8104;urp; --nofirststartwizard
命令参考:https://help.libreoffice.org/Common/Starting_the_Software_With_Parameters/zh-CN
2.2.5. 手动启动 如果提示 缺少 *.so 文件 系系统缺乏 LibreOffice 相关依 赖 执行命令:apt install libreoffice 等待部署完成 再手动启动
3. 字体安装
3.1. 参考链接
https://www.jianshu.com/p/e7f12b8c8602
3.2. 中文字体安装
- cd /usr/share/fonts 执行命令 fc-list 查看系统字体 是否有中文字体
- 将 C:\Windows\Fonts 文件夹复制到桌面命名 chinese (安装 win 系统字体)
- 在服务器上 cd /usr/share/fonts 上传文件夹 等待上传完成
- cd chinese
- mkfontscale
- mkfontdir
- cd /etc/fonts , vim fonts.conf
新增 后保存 - 执行 source fonts.conf
- cd /usr/share/fonts/chinese 执行 fc-cache -fv 等待执行完成
- 执行 fc-list 查看字体 有上传的字体则安装成功
4. API
4.1. 配置文件中配置依赖
jodconverter:
local:
enabled: true
office-home: D:/LibreOffice
portNumbers: 8103,8101,8102
maxTasksPerProcess: 100
注:office-home 值为 本地 libreOffice 安装目录 ,portNumbers 为开放端口可设置端口进行多个进程执行
更 多 问 题 参 考 jobconverter 社 区
4.2. 调用方法
@Resource private DocumentConverter documentConverter; // 具 体 转 换 方 法 参 数 是 java.io.File
@Resource
private LocalOfficeManager officeManager ;
try {
File pptFile = new File("E:\\test\\05250500.html");
File pdfFile = new File("E:\\test\\05250500.pdf");
documentConverter.convert(pptFile).to(pdfFile).execute();
} catch (OfficeException e) {
e.printStackTrace();
} finally {
officeManager.stop();
}
convert 方法 接受参数 java.io.File 或 java.io.InputStream
to 方法 接受参数 java.io.File 或 java.io.OutputStream
4.3. 在线 API
https://javadoc.io/doc/org.jodconverter/jodconverter-online/4.2.0/ind ex.html