javascript
SpringBoot使用LibreOffice转换PDF
用LibreOffice將Office文檔轉(zhuǎn)換PDF
本例使用 LibreOffice-6.0.4、jodconverter-4.2.0、spring-boot-1.5.9.RELEASE。
在CentOS7 + openJDK8 和 Windows7 + jdk1.8 環(huán)境下測試通過。
LibreOffice轉(zhuǎn)換命令
# 源文件放在 e:\tmp\123.docx 或者 /tmp/123.docx# 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 /tmpjodconverter封裝了一組轉(zhuǎn)換命令,通過java調(diào)用LibreOffice相關服務。
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的遠程調(diào)用。
配置 application.properties
jodconverter.local.enabled=true # 設置LibreOffice主目錄 jodconverter.local.office-home=${pom.office.home} # 開啟多個LibreOffice進程,每個端口對應一個進程 jodconverter.local.portNumbers=8100,8101,8102 # LibreOffice進程重啟前的最大進程數(shù) jodconverter.local.maxTasksPerProcess=100使用Maven的多環(huán)境配置
<profiles><profile><!-- windows環(huán)境 --><id>win</id><activation><activeByDefault>true</activeByDefault></activation><properties><pom.office.home>C:/Program Files/LibreOffice</pom.office.home></properties></profile><profile><!-- linux環(huán)境 --><id>linux</id><properties><pom.office.home>/opt/libreoffice6.0</pom.office.home></properties></profile> </profiles>調(diào)用方法
import org.jodconverter.DocumentConverter;@Resource private DocumentConverter documentConverter;// 具體轉(zhuǎn)換方法,參數(shù)是java.io.File documentConverter.convert(sourceFile).to(targetFile).execute();- convert方法 接受參數(shù) java.io.File 或 java.io.InputStream
- to方法 接受參數(shù) java.io.File 或 java.io.OutputStream
在線預覽
使用開源項目 https://github.com/mozilla/pdf.js
下載最新的release包(pdfjs-x.y.z-dist.zip)
pdfjs\web\viewer.html傳入?yún)?shù)file(示例:http://localhost:8080/pdfjs/web/viewer.html?file=xxxxxxx.pdf)
CentOS7安裝LibreOffice
官網(wǎng)
https://zh-cn.libreoffice.org/
科大鏡像
http://mirrors.ustc.edu.cn/td...
中文語言包
http://mirrors.ustc.edu.cn/td...
下載安裝包
wget -P /tmp/office http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.0.4/rpm/x86_64/LibreOffice_6.0.4_Linux_x86-64_rpm.tar.gzwget -P /tmp/office http://mirrors.ustc.edu.cn/tdf/libreoffice/stable/6.0.4/rpm/x86_64/LibreOffice_6.0.4_Linux_x86-64_rpm_langpack_zh-CN.tar.gz解壓縮
tar zxvf /tmp/office/LibreOffice_6.0.4_Linux_x86-64_rpm.tar.gz -C /tmp/officetar zxvf /tmp/office/LibreOffice_6.0.4_Linux_x86-64_rpm_langpack_zh-CN.tar.gz -C /tmp/office檢查安裝包
ll /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm/RPMS/*.rpmll /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm用yum安裝,不要執(zhí)行install
yum install /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm/RPMS/*.rpmyum install /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm安裝libcairo.so.2依賴庫
yum install ibus查找服務目錄
安裝路徑:/opt/libreoffice6.0
快捷方式:/usr/bin/libreoffice6.0
啟動服務
/usr/bin/libreoffice6.0 --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizardCentOS7安裝字體庫
在CentOS7服務器上,利用LibreOffice將word等格式轉(zhuǎn)換為PDF,發(fā)現(xiàn)不支持漢字。需要安裝字體庫。
安裝fontconfig
yum -y install fontconfig安裝完成后,/usr/share目錄就可以看到fonts和fontconfig兩個目錄。
安裝ttmkfdir
yum -y install ttmkfdir檢查已有字體庫
fc-list復制字體
#新建文件夾 mkdir /usr/share/fonts/chinese把Windows系統(tǒng)的字體C:\Windows\Fonts復制進去。
- simsun.ttc 宋體
- simhei.ttf 黑體
- msyh.ttf 微軟雅黑
- msyhbd.ttf 微軟雅黑
匯總生成fonts.scale文件
ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir修改字體配置文件
vim /etc/fonts/fonts.conf修改內(nèi)容
<fontconfig>....<dir>....<dir>/usr/share/fonts/chinese</dir>.... </fontconfig>更新字體緩存
fc-cache -fv總結
以上是生活随笔為你收集整理的SpringBoot使用LibreOffice转换PDF的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 前端学习记录(CSS篇)
- 下一篇: 利刃 MVVMLight 7:命令深入