SpringBoot整合PDF导出功能

在实际开发中,我们经常需要将数据导出为PDF格式,以便于打印、分享或存档。SpringBoot提供了多种方式来实现PDF导出功能,下面我们将介绍其中的一些。

HTML 模板转 PDF(推荐)

通过模板引擎(如 Thymeleaf 或 Freemarker)生成 HTML,再转换为 PDF。

1.首先,需要在pom.xml文件中添加依赖项:
复制代码
<!-- Thymeleaf 模板引擎 -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.12.RELEASE</version> <!-- 请检查并使用最新版本 -->
        </dependency>
        <dependency>
            <groupId>org.xhtmlrenderer</groupId>
            <artifactId>flying-saucer-pdf-itext5</artifactId>
            <version>9.1.20</version> <!-- 请检查并使用最新版本 -->
        </dependency>
2.接下来,我们需要把模板文件pdfTemplate.html放到资产文件夹中 src/main/resources/templates
复制代码
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <style>
        body { font-family: SimSun; } /* 解决中文乱码 */
    </style>

</head>
<body>
<div>
    <span th:text="'名称:' + ${title}" style="display: block;"></span>
</div>



</body>
</html>
3.还需要把对应的字体放到resource中,这样防止中文在pdf中不显示,放在该路径下 src/main/resources/fonts/simsun.ttc
4.接下来,创建一个Controller类,用于生成PDF文件:
复制代码
package com.onejson.ojmall.controller;

import org.thymeleaf.TemplateEngine;
import com.itextpdf.text.pdf.BaseFont;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.thymeleaf.context.Context;
import org.xhtmlrenderer.pdf.ITextRenderer;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/pdf")
@Api(tags = "pdf管理")
public class PdfController {


    /**
     * 通过html模板生成pdf
     * @param response
     * @throws Exception
     */
    @GetMapping("/export/html2pdf")
    public void export(HttpServletResponse response) throws Exception {
        // 设置响应头
        response.setContentType("application/pdf");
        response.setHeader("Content-Disposition", "attachment; filename=report.pdf");

        // 加载HTML模板
        InputStream templateInputStream = this.getClass().getResourceAsStream("/templates/pdfTemplate.html");
        assert templateInputStream != null;
        byte[] bytes = new byte[templateInputStream.available()];
        templateInputStream.read(bytes);
        String htmlContent = new String(bytes, StandardCharsets.UTF_8);

        // 准备上下文数据
        Context context = new Context();

        context.setVariable("title", "测试");

        // 渲染HTML
        TemplateEngine templateEngine = new TemplateEngine();
        String processedHtml = templateEngine.process(htmlContent, context);

        // 将HTML转换为PDF
        ITextRenderer renderer = new ITextRenderer();
        renderer.setDocumentFromString(processedHtml);
        // 字段导入很重要,不然中文不显示
        renderer.getFontResolver().addFont("/fonts/simsun.ttc", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
        renderer.layout();

        // 输出PDF
        try (OutputStream outputStream = response.getOutputStream()) {
            renderer.createPDF(outputStream);
        }
    }
}

这里我们设置了一个 title的变量来测试内容。

5.最后利用Postman工具请求接口,得到结果显示。完成对接
相关推荐
uzong1 分钟前
企业智能助手的实践分享(LLM/RAG)
后端·程序员·架构
m0_547486665 分钟前
华南农业大学《C语言程序设计》期末试卷及答案2018-2025年PDF
c语言·开发语言·pdf·c语言程序设计
GetcharZp8 小时前
GitHub 49K+ Star!C++ 开发者必知的 JSON 神级库:从零到精通全指北
后端
xujinwei_gingko8 小时前
SpringBoot整合WebSocket
spring boot·后端·websocket
智码看视界9 小时前
现代Web开发基础:全栈工程师的起航点
前端·后端·c5全栈
程序员cxuan9 小时前
Claude Fable 5 来了
人工智能·后端·程序员
JS菌9 小时前
手写一个 AI Agent 全栈项目:从沙箱执行到子智能体的完整实现
前端·人工智能·后端
wang09079 小时前
自己动手写一个spring之IOC_2
java·后端·spring
来杯@Java10 小时前
学生选课管理系统(基于springboot+vue前后端分离的项目)计算机毕业设计java
java·spring boot·spring·vue·毕业设计·maven·mybatis
ltl10 小时前
推理退化:为什么大模型会输出乱码、死循环和无意义文本
后端