SpringBoot使用LibreOffice轉換PDF

用LibreOffice將Office文檔轉換PDF

本例使用 LibreOffice-6.0.4jodconverter-4.2.0spring-boot-1.5.9.RELEASE
CentOS7 + openJDK8Windows7 + jdk1.8 環境下測試經過。html

LibreOffice轉換命令

# 源文件放在 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

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>

注意: 在這裏說明特別一下,jodconverter4.2開始,對LibreOffice相關功能從jodconverter-core中分離出來,封裝到爲jodconverter-local,另外新增了jodconverter-online,支持LibreOffice online server的遠程調用。linux

配置 application.properties

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.Filejava.io.InputStream
  • to方法 接受參數 java.io.Filejava.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.pdfweb

CentOS7安裝LibreOffice

官網
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

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

安裝libcairo.so.2依賴庫

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安裝字體庫

在CentOS7服務器上,利用LibreOffice將word等格式轉換爲PDF,發現不支持漢字。須要安裝字體庫。

安裝fontconfig

yum -y install fontconfig

安裝完成後,/usr/share目錄就能夠看到fonts和fontconfig兩個目錄。

安裝ttmkfdir

yum -y install ttmkfdir

檢查已有字體庫

fc-list

複製字體

#新建文件夾
mkdir /usr/share/fonts/chinese

把Windows系統的字體C:\Windows\Fonts複製進去。

  • simsun.ttc 宋體
  • simhei.ttf 黑體
  • msyh.ttf 微軟雅黑
  • msyhbd.ttf 微軟雅黑
# 修改字體權限
chmod -R 644 /usr/share/fonts/chinese

彙總生成fonts.scale文件

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
相關文章
相關標籤/搜索