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 方案对中文支持可能需要额外配置字体。

相关推荐
markfeng81 天前
Python+Django+H5+MySQL项目搭建
python·django
用户8307196840821 天前
Spring Boot 项目中日期处理的最佳实践
java·spring boot
GinoWi1 天前
Chapter 2 - Python中的变量和简单的数据类型
python
JordanHaidee1 天前
Python 中 `if x:` 到底在判断什么?
后端·python
ServBay1 天前
10分钟彻底终结冗长代码,Python f-string 让你重获编程自由
后端·python
闲云一鹤1 天前
Python 入门(二)- 使用 FastAPI 快速生成后端 API 接口
python·fastapi
Rockbean1 天前
用40行代码搭建自己的无服务器OCR
服务器·python·deepseek
曲幽1 天前
FastAPI + Ollama 实战:搭一个能查天气的AI助手
python·ai·lora·torch·fastapi·web·model·ollama·weatherapi
用户60648767188961 天前
国内开发者如何接入 Claude API?中转站方案实战指南(Python/Node.js 完整示例)
人工智能·python·api
只与明月听1 天前
RAG深入学习之Chunk
前端·人工智能·python