本例使用 LibreOffice-6.0.4
、jodconverter-4.2.0
、spring-boot-1.5.9.RELEASE
。
在CentOS7 + openJDK8
和 Windows7 + jdk1.8
環境下測試經過。html
# 源文件放在 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 /tmp
jodconverter
封裝了一組轉換命令,經過java
調用LibreOffice
相關服務。java
<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
的遠程調用。linux
jodconverter.local.enabled=true # 設置LibreOffice主目錄 jodconverter.local.office-home=${pom.office.home} # 開啓多個LibreOffice進程,每一個端口對應一個進程 jodconverter.local.portNumbers=8100,8101,8102 # LibreOffice進程重啓前的最大進程數 jodconverter.local.maxTasksPerProcess=100
使用Maven
的多環境配置git
<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>
import org.jodconverter.DocumentConverter; @Resource private DocumentConverter documentConverter; // 具體轉換方法,參數是java.io.File documentConverter.convert(sourceFile).to(targetFile).execute();
convert方法
接受參數 java.io.File
或 java.io.InputStream
to方法
接受參數 java.io.File
或 java.io.OutputStream
使用開源項目 https://github.com/mozilla/pdf.js
下載最新的release包(pdfjs-x.y.z-dist.zip)github
pdfjs\web\viewer.html
傳入參數file
(示例:http://localhost:8080/pdfjs/web/viewer.html?file=xxxxxxx.pdf
)web
官網
https://zh-cn.libreoffice.org/spring
科大鏡像
http://mirrors.ustc.edu.cn/td...shell
中文語言包
http://mirrors.ustc.edu.cn/td...vim
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.gz 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_langpack_zh-CN.tar.gz
tar zxvf /tmp/office/LibreOffice_6.0.4_Linux_x86-64_rpm.tar.gz -C /tmp/office tar 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/*.rpm ll /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm
yum install /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm/RPMS/*.rpm yum install /tmp/office/LibreOffice_6.0.4.2_Linux_x86-64_rpm_langpack_zh-CN/RPMS/*.rpm
yum install ibus
安裝路徑:/opt/libreoffice6.0
快捷方式:/usr/bin/libreoffice6.0windows
/usr/bin/libreoffice6.0 --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard
在CentOS7服務器上,利用LibreOffice將word等格式轉換爲PDF,發現不支持漢字。須要安裝字體庫。
yum -y install fontconfig
安裝完成後,/usr/share目錄就能夠看到fonts和fontconfig兩個目錄。
yum -y install ttmkfdir
fc-list
#新建文件夾 mkdir /usr/share/fonts/chinese
把Windows系統的字體C:\Windows\Fonts
複製進去。
# 修改字體權限 chmod -R 644 /usr/share/fonts/chinese
ttmkfdir -e /usr/share/X11/fonts/encodings/encodings.dir
vim /etc/fonts/fonts.conf
<fontconfig> .... <dir>.... <dir>/usr/share/fonts/chinese</dir> .... </fontconfig>
fc-cache -fv