SpringBoot使用Docx4j實(shí)現(xiàn)DOCX轉(zhuǎn)PDF功能
前言
在當(dāng)今的企業(yè)級(jí)應(yīng)用中,文檔格式轉(zhuǎn)換是一個(gè)高頻但又容易被低估的需求。從合同簽署、報(bào)表生成到知識(shí)庫(kù)管理,DOCX轉(zhuǎn)PDF的需求無(wú)處不在。市面上的解決方案五花八門,但真正能平衡"成本、質(zhì)量、可維護(hù)性"的方案卻寥寥無(wú)幾。今天我們就介紹一款使用純Java實(shí)現(xiàn) DOCX 轉(zhuǎn) PDF的方案,超級(jí)簡(jiǎn)單,簡(jiǎn)直不要太爽。

Docx4j的核心優(yōu)勢(shì)
Docx4j之所以成為企業(yè)級(jí)應(yīng)用的首選方案,主要得益于其以下核心優(yōu)勢(shì):
1.純Java實(shí)現(xiàn):無(wú)需安裝任何外部軟件,部署簡(jiǎn)單
2.開(kāi)源免費(fèi):采用Apache 2.0 License,可商用
3.樣式保真度高:能完美保留Word文檔中的圖片、表格、頁(yè)眉頁(yè)腳等復(fù)雜格式
4.易于集成:可以無(wú)縫集成到SpringBoot項(xiàng)目中
實(shí)戰(zhàn)演示
項(xiàng)目依賴配置
在pom.xml中添加docx4j相關(guān)依賴:
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-core</artifactId>
<!-- 使用兼容 Java 8 的版本 Java 11 11.4.8-->
<version>8.3.4</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-JAXB-ReferenceImpl</artifactId>
<version>8.3.4</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j-export-fo</artifactId>
<version>8.3.4</version>
</dependency>
核心工具類
創(chuàng)建DocxToPdfUtil工具類,封裝DOCX轉(zhuǎn)PDF的核心邏輯:
/**
* DocxToPdfUtil
* @author senfel
* @version 1.0
* @date 2026/2/4 17:21
*/
@Slf4j
public class DocxToPdfUtil {
/**
* 將docx文件轉(zhuǎn)換為 PDF
* @param docxPath
* @param pdfPath
* @author senfel
* @date 2026/2/4 17:22
* @return void
*/
public static void convert(String docxPath, String pdfPath) {
try {
// 1. 加載 Word 文檔
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(docxPath));
// 2. 配置字體映射(防止中文亂碼)
Mapper fontMapper = new IdentityPlusMapper();
PhysicalFonts.discoverPhysicalFonts();
PhysicalFont simsun = PhysicalFonts.get("SimSun");
if (simsun != null) {
fontMapper.put("SimSun", simsun);
// 常用中文字體映射表
fontMapper.put("隸書(shū)", PhysicalFonts.get("LiSu"));
fontMapper.put("宋體", PhysicalFonts.get("SimSun"));
fontMapper.put("微軟雅黑", PhysicalFonts.get("Microsoft YaHei"));
fontMapper.put("黑體", PhysicalFonts.get("SimHei"));
fontMapper.put("楷體", PhysicalFonts.get("KaiTi"));
fontMapper.put("新宋體", PhysicalFonts.get("NSimSun"));
fontMapper.put("華文行楷", PhysicalFonts.get("STXingkai"));
fontMapper.put("華文仿宋", PhysicalFonts.get("STFangsong"));
fontMapper.put("仿宋", PhysicalFonts.get("FangSong"));
fontMapper.put("幼圓", PhysicalFonts.get("YouYuan"));
fontMapper.put("華文宋體", PhysicalFonts.get("STSong"));
fontMapper.put("華文中宋", PhysicalFonts.get("STZhongsong"));
fontMapper.put("等線", PhysicalFonts.get("SimSun"));
fontMapper.put("等線 Light", PhysicalFonts.get("SimSun"));
fontMapper.put("華文琥珀", PhysicalFonts.get("STHupo"));
fontMapper.put("華文隸書(shū)", PhysicalFonts.get("STLiti"));
fontMapper.put("華文新魏", PhysicalFonts.get("STXinwei"));
fontMapper.put("華文彩云", PhysicalFonts.get("STCaiyun"));
fontMapper.put("方正姚體", PhysicalFonts.get("FZYaoti"));
fontMapper.put("方正舒體", PhysicalFonts.get("FZShuTi"));
fontMapper.put("華文細(xì)黑", PhysicalFonts.get("STXihei"));
fontMapper.put("宋體擴(kuò)展", PhysicalFonts.get("simsun-extB"));
fontMapper.put("仿宋_GB2312", PhysicalFonts.get("FangSong_GB2312"));
fontMapper.put("新細(xì)明體", PhysicalFonts.get("SimSun"));
// 修復(fù)體(正文)/宋體(標(biāo)題亂碼
PhysicalFonts.put("PMingLiU", PhysicalFonts.get("SimSun"));
PhysicalFonts.put("新細(xì)明體", PhysicalFonts.get("SimSun"));
wordMLPackage.setFontMapper(fontMapper);
}
// 3. 創(chuàng)建輸出流并執(zhí)行轉(zhuǎn)換
try (FileOutputStream os = new FileOutputStream(pdfPath)) {
Docx4J.toPDF(wordMLPackage, os);
}
log.info("PDF 生成成功:{}" ,pdfPath);
} catch (Exception e) {
log.error("轉(zhuǎn)換失?。簕}",e.getMessage(),e);
}
}
}
測(cè)試用例
創(chuàng)建類DocxToPdfTest,驗(yàn)證提供DOCX轉(zhuǎn)PDF的工具類:
/**
* DocxToPdfTest
* @author senfel
* @version 1.0
* @date 2026/2/4 17:26
*/
@SpringBootTest
public class DocxToPdfTest {
/**
* covertTest
* @author senfel
* @date 2026/2/4 17:27
* @return void
*/
@Test
public void covertTest() {
String docxPath = "D:\\blank\\小程序簡(jiǎn)介中英文.docx";
String pdfPath = "D:\\blank\\小程序簡(jiǎn)介中英文.pdf";
DocxToPdfUtil.convert(docxPath, pdfPath);
}
}

解決中文亂碼
Windows
在Windows環(huán)境中,中文亂碼主要是由于字體映射問(wèn)題導(dǎo)致的。我們需要在工具類中添加中文字體映射表,確保docx4j能夠正確識(shí)別和轉(zhuǎn)換中文字體。
Linux
在Linux環(huán)境中,中文亂碼問(wèn)題更為復(fù)雜,需要安裝Windows字體。具體步驟如下:
1.新建字體文件夾:
sudo mkdir -p /usr/share/fonts/win_font
2.拷貝Windows字體文件:將Windows系統(tǒng)中路徑為C:\Windows\Fonts的字體文件拷貝到Linux的/usr/share/fonts/win_font目錄中。

3.加載字體文件:
cd /usr/share/fonts/win_font sudo mkfontscale # 生成字體縮放文件 sudo mkfontdir # 生成字體目錄索引 sudo fc-cache -fv # 刷新字體緩存
4.驗(yàn)證字體安裝:
fc-list :lang=zh
總結(jié)
Docx4j是一個(gè)優(yōu)秀的開(kāi)源文檔處理工具,它為SpringBoot項(xiàng)目提供了一種輕量級(jí)、高性能的DOCX轉(zhuǎn)PDF解決方案。通過(guò)本文的介紹,我們了解了docx4j的核心優(yōu)勢(shì)、并使用springboot實(shí)戰(zhàn)落了一個(gè)簡(jiǎn)單的案例。在實(shí)際項(xiàng)目中,我們可以根據(jù)業(yè)務(wù)需求和技術(shù)選型戰(zhàn)略,選擇最適合的文檔轉(zhuǎn)換方案。對(duì)于大多數(shù)企業(yè)級(jí)應(yīng)用來(lái)說(shuō),Docx4j無(wú)疑是一個(gè)性價(jià)比極高的選擇。
以上就是SpringBoot使用Docx4j實(shí)現(xiàn)DOCX轉(zhuǎn)PDF功能的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot DOCX轉(zhuǎn)PDF的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Springmvc和ajax如何實(shí)現(xiàn)前后端交互
這篇文章主要介紹了Springmvc和ajax如何實(shí)現(xiàn)前后端交互,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
解決創(chuàng)建springboot后啟動(dòng)報(bào)錯(cuò):Failed?to?bind?properties?under‘spri
在Spring?Boot項(xiàng)目中,application.properties和application.yml是用于配置參數(shù)的兩種文件格式,properties格式簡(jiǎn)潔但不支持層次結(jié)構(gòu),而yml格式支持層次性,可讀性更好,在yml文件中,要注意細(xì)節(jié),比如冒號(hào)后面需要空格2024-10-10
idea中mapper如何快速跳轉(zhuǎn)到xml插件
這篇文章主要介紹了idea中mapper如何快速跳轉(zhuǎn)到xml插件問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
SpringBoot利用自定義注解實(shí)現(xiàn)多數(shù)據(jù)源
這篇文章主要為大家詳細(xì)介紹了SpringBoot如何利用自定義注解實(shí)現(xiàn)多數(shù)據(jù)源效果,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,需要的可以了解一下2022-10-10

