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

相关推荐
alvin_20055 分钟前
python之OpenGL应用(二)Hello Triangle
python·opengl
机器视觉的发动机11 分钟前
AI算力中心的能耗挑战与未来破局之路
开发语言·人工智能·自动化·视觉检测·机器视觉
韩立学长13 分钟前
基于Springboot泉州旅游攻略平台d5h5zz02(程序、源码、数据库、调试部署方案及开发环境)系统界面展示及获取方式置于文档末尾,可供参考。
数据库·spring boot·旅游
铁蛋AI编程实战14 分钟前
通义千问 3.5 Turbo GGUF 量化版本地部署教程:4G 显存即可运行,数据永不泄露
java·人工智能·python
HyperAI超神经18 分钟前
在线教程|DeepSeek-OCR 2公式/表格解析同步改善,以低视觉token成本实现近4%的性能跃迁
开发语言·人工智能·深度学习·神经网络·机器学习·ocr·创业创新
jiang_changsheng26 分钟前
RTX 2080 Ti魔改22GB显卡的最优解ComfyUI教程
python·comfyui
R_.L28 分钟前
【QT】常用控件(按钮类控件、显示类控件、输入类控件、多元素控件、容器类控件、布局管理器)
开发语言·qt
Zach_yuan37 分钟前
自定义协议:实现网络计算器
linux·服务器·开发语言·网络
摇滚侠38 分钟前
在 SpringBoot 项目中,开发工具使用 IDEA,.idea 目录下的文件需要提交吗
java·spring boot·intellij-idea
云姜.43 分钟前
java多态
java·开发语言·c++