日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot使用LibreOffice转换PDF

發布時間:2025/6/15 javascript 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot使用LibreOffice转换PDF 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

用LibreOffice將Office文檔轉換PDF

本例使用 LibreOffice-6.0.4、jodconverter-4.2.0、spring-boot-1.5.9.RELEASE。
在CentOS7 + openJDK8 和 Windows7 + jdk1.8 環境下測試通過。

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相關服務。

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的遠程調用。

配置 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的多環境配置

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

pdfjs\web\viewer.html傳入參數file(示例:http://localhost:8080/pdfjs/web/viewer.html?file=xxxxxxx.pdf)

CentOS7安裝LibreOffice

官網
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安裝,不要執行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;" --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

總結

以上是生活随笔為你收集整理的SpringBoot使用LibreOffice转换PDF的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。