Spring Boot 实现 DOCX 转 PDF

使用 Apache POI 和 Apache PDFBox 实现 DOCX 转 PDF

Apache POI 用于读取 DOCX 文件内容,Apache PDFBox 用于生成 PDF 文件。需添加以下依赖:

XML 复制代码
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>5.2.3</version>
</dependency>
<dependency>
    <groupId>org.apache.pdfbox</groupId>
    <artifactId>pdfbox</artifactId>
    <version>2.0.27</version>
</dependency>

代码实现:

java 复制代码
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.font.PDType1Font;

import java.io.FileInputStream;
import java.io.FileOutputStream;

public class DocxToPdfConverter {
    public static void convert(String docxPath, String pdfPath) throws Exception {
        try (XWPFDocument doc = new XWPFDocument(new FileInputStream(docxPath));
             PDDocument pdfDoc = new PDDocument()) {
            
            PDPage page = new PDPage();
            pdfDoc.addPage(page);
            
            try (PDPageContentStream contentStream = new PDPageContentStream(pdfDoc, page)) {
                contentStream.setFont(PDType1Font.HELVETICA, 12);
                contentStream.beginText();
                contentStream.newLineAtOffset(25, 700);
                
                for (XWPFParagraph paragraph : doc.getParagraphs()) {
                    contentStream.showText(paragraph.getText());
                    contentStream.newLineAtOffset(0, -15);
                }
                
                contentStream.endText();
            }
            
            pdfDoc.save(pdfPath);
        }
    }
}

使用 LibreOffice 命令行转换

通过调用 LibreOffice 的命令行工具实现格式转换,需先安装 LibreOffice:

java 复制代码
public class LibreOfficeConverter {
    public static void convert(String inputPath, String outputPath) throws Exception {
        String command = "libreoffice --headless --convert-to pdf " + inputPath + " --outdir " + outputPath;
        Process process = Runtime.getRuntime().exec(command);
        process.waitFor();
    }
}

使用第三方库 docx4j 和 flying-saucer-pdf

docx4j 处理 DOCX 文件,flying-saucer-pdf 生成 PDF:

XML 复制代码
<dependency>
    <groupId>org.docx4j</groupId>
    <artifactId>docx4j</artifactId>
    <version>11.4.4</version>
</dependency>
<dependency>
    <groupId>org.xhtmlrenderer</groupId>
    <artifactId>flying-saucer-pdf</artifactId>
    <version>9.1.22</version>
</dependency>

转换代码:

java 复制代码
import org.docx4j.Docx4J;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import java.io.FileOutputStream;

public class Docx4jConverter {
    public static void convert(String docxPath, String pdfPath) throws Exception {
        WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(docxPath));
        Docx4J.toPDF(wordMLPackage, new FileOutputStream(pdfPath));
    }
}

注意事项

DOCX 转 PDF 的保真度取决于所选工具。Apache POI 方案对复杂格式支持有限,LibreOffice 转换效果最佳但需安装软件,docx4j 方案对中文支持可能需要额外配置字体。

相关推荐
阿坤带你走近大数据2 小时前
JavaScript脚本语言的简单介绍
开发语言·javascript·ecmascript
小二·2 小时前
Python Web 开发进阶实战:前端现代化 —— Vue 3 + TypeScript 重构 Layui 界面,打造高性能 SPA
前端·python·typescript
悟道|养家2 小时前
基于L1/L2 缓存访问速度的角度思考数组和链表的数据结构设计以及工程实践方案选择(2)
java·开发语言·缓存
wjs20242 小时前
堆的基本存储
开发语言
虫小宝2 小时前
微信群发消息API接口对接中Java后端的请求参数校验与异常反馈优化技巧
android·java·开发语言
万行2 小时前
机器学习&第六.七章决策树,集成学习
人工智能·python·算法·决策树·机器学习·集成学习
麦兜*2 小时前
Spring Boot整合Swagger 3.0:自动生成API文档并在线调试
java·spring boot·后端
Main. 242 小时前
从0到1学习Qt -- Qt3D入门
开发语言·qt·学习
接着奏乐接着舞。2 小时前
Go 一小时上手指南:从零到运行第一个程序
开发语言·后端·golang