SpringBoot使用LibreOffice實現(xiàn)高保真Word轉PDF的方法
一、需要先安裝LibreOffice
Windows:
訪問官網(wǎng):https://www.libreoffice.org/download/,下載Windows版本安裝包,雙擊運行安裝,按提示完成安裝,默認安裝路徑:C:\Program
Files\LibreOffice
Mac:
# 方式1:Homebrew安裝 brew install --cask libreoffice # 方式2:官網(wǎng)下載DMG安裝
Linux服務器安裝:
1.Ubuntu/Debian
# 更新包列表 sudo apt update # 安裝LibreOffice sudo apt install libreoffice # 安裝中文字體(可選) sudo apt install fonts-wqy-zenhei fonts-wqy-microhei # 驗證安裝 libreoffice --version
2.CentOS/RHEL:
# 添加EPEL倉庫 sudo yum install epel-release # 安裝LibreOffice sudo yum install libreoffice # 安裝中文字體 sudo yum install wqy-zenhei-fonts
查看安裝路徑
which libreoffice # 通常路徑:/usr/lib/libreoffice
Docker中安裝:
# Dockerfile
FROM openjdk:11-jre-slim
# 安裝LibreOffice
RUN apt update && \
apt install -y libreoffice && \
apt clean && \
rm -rf /var/lib/apt/lists/*
# 設置環(huán)境變量
ENV OFFICE_HOME=/usr/lib/libreoffice
驗證安裝是否成功
# 查看版本 libreoffice --version # 啟動服務測試 soffice --headless --accept="socket,host=0.0.0.0,port=2002;urp;" --nofirststartwizard # 查看進程 ps aux | grep soffice
常見問題解決
字體缺失:
# Linux安裝常用字體 sudo apt install fonts-liberation fonts-noto-cjk
端口占用:
# 查看端口占用 netstat -tlnp | grep 2002 # 修改SpringBoot配置中的端口號
內存優(yōu)化(服務器環(huán)境):
# application.yml
jodconverter:
local:
max-tasks-per-process: 20 # 根據(jù)服務器性能調整
task-execution-timeout: 120000 # 超時時間(ms)
二、使用示例
添加依賴
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-spring-boot-starter</artifactId>
<version>4.4.6</version>
</dependency>
<dependency>
<groupId>org.jodconverter</groupId>
<artifactId>jodconverter-local</artifactId>
<version>4.4.6</version>
</dependency>
配置application.yml
jodconverter:
local:
enabled: true
office-home: /usr/lib/libreoffice # Linux
# office-home: C:\Program Files\LibreOffice # Windows
port-numbers: 2002
max-tasks-per-process: 100
代碼示例:
package com.geofly.geocloud.queryapp.intranet.service;
import org.jodconverter.core.DocumentConverter;
import org.jodconverter.core.office.OfficeException;
import org.springframework.stereotype.Service;
import java.io.File;
/**
* word轉pdf
*/
@Service
public class WordToPdfService {
private final DocumentConverter documentConverter;
public WordToPdfService(DocumentConverter documentConverter) {
this.documentConverter = documentConverter;
}
/**
* word轉pdf
* @param wordFile
* @param pdfFile
* @throws OfficeException
*/
public void wordToPdf(File wordFile, File pdfFile) throws OfficeException {
documentConverter.convert(wordFile)
.to(pdfFile)
.execute();
}
}
簡單使用示例
// 將DOCX轉換為PDF
try {
wordToPdfService.wordToPdf(docxFile, new File(upLoadPath, pdfFilePath));
} catch (Exception e) {
OpLogUtil.info("轉pdf操作出錯了:" + e.getMessage());
return "轉pdf操作出錯了:" + e.getMessage() + ",pdfFilePath:" + pdfFilePath;
}
非常的好用!

注意:
確保系統(tǒng)已安裝LibreOffice
默認端口2002,確保端口未被占用
生產(chǎn)環(huán)境建議配置連接池參數(shù)
以上就是SpringBoot使用LibreOffice實現(xiàn)高保真Word轉PDF的方法的詳細內容,更多關于SpringBoot LibreOffice高保真Word轉PDF的資料請關注腳本之家其它相關文章!
相關文章
SpringBoot混合使用StringRedisTemplate和RedisTemplate的坑及解決
這篇文章主要介紹了SpringBoot混合使用StringRedisTemplate和RedisTemplate的坑及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
使用Mybatis-plus實現(xiàn)時間自動填充(代碼直接可用)
這篇文章主要介紹了使用Mybatis-plus實現(xiàn)時間自動填充(代碼直接可用),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
SpringBoot啟動報錯Whitelabel Error Page: This&nbs
當我們使用Spring Boot框架開發(fā)Web應用時,有時會遇到啟動報錯信息為"Whitelabel Error Page: This application has no explicit mapping for",種報錯信息意味著我們的應用缺少某個URL映射的配置,導致請求無法處理,在本篇文章中,我們將詳細討論如何解決這個問題2024-03-03

