java 结合libreoffice 做文档转换,word转pdf

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+)

  1. 上 传 对 应 系 统 的 压 缩 包 到 系 统 指 定 目 录
    在这里插入图片描述
  2. 解压对应压缩包
  3. tar -xvf LibreOffice_6.2.8_Linux_x86-64_deb.tar.gz
  4. tar -xvf LibreOffice_6.2.8_Linux_x86-64_deb_langpack_zh-CN.tar.gz
  5. 解压后文件
    在这里插入图片描述
  6. cd LibreOffice_6.2.8.2_Linux_x86-64_deb
    在这里插入图片描述
  7. 进入DEBS 文件夹 cd DEBS/ 执行命令 sudo dpkg -i *.deb
  8. 回退带解压目录 执行 cd LibreOffice_6.2.8.2_Linux_x86-64_deb_langpack_zh-CN/
    在这里插入图片描述
  9. 进入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. 中文字体安装

  1. cd /usr/share/fonts 执行命令 fc-list 查看系统字体 是否有中文字体
  2. 将 C:\Windows\Fonts 文件夹复制到桌面命名 chinese (安装 win 系统字体)
  3. 在服务器上 cd /usr/share/fonts 上传文件夹 等待上传完成
  4. cd chinese
  5. mkfontscale
  6. mkfontdir
  7. cd /etc/fonts , vim fonts.conf
    新增 后保存
  8. 执行 source fonts.conf
  9. cd /usr/share/fonts/chinese 执行 fc-cache -fv 等待执行完成
  10. 执行 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

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值