2023最新SpringBoot导出PDF方式(模板方式)

一、前期准备


在开发中经常会遇到需要进行对一些数据进行动态导出PDF文件,然后让用户自己选择是否需要打印出来,这篇文章我们来用个相对来说比较简单的方式来实现PDF动态导出;
导入依赖 SpringBoot版本2.0.5.RELEASE

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-freemarker</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.20</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>html2pdf</artifactId>
            <version>4.0.3</version>
        </dependency>

二、代码实现) 先准备一个html,这个html是一个模板,是将我们需要动态展示的数据插入到每个占位符进来,如下:


先准备一个html,这个html是一个模板,是将我们需要动态展示的数据插入到每个占位符进来,如下:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <title>Title</title>
    <style>
        body{
            font-size: 15px;
        }
        .title{
            text-align: center;
        }
        .content{
            margin:0 auto;
            width: 400px;
        }
        .content .text{
            text-indent: 2em;
        }
        .content .datetime{
            text-align: right;
        }
    </style>
</head>
<body>
<div>
    <div class="view">
        <h2 class="title">自我介绍</h2>
        <div class="content">
            <p class="text">
                大家好,我叫${person.personName},我今年${person.personAge},我是个${person.personGender},
                我的职业是${person.personVocation},我目前住在${person.address},我在性格方面${person.personalityDesc}。
            </p>
            <p class="datetime">${person.createTime}</p>
        </div>
    </div>
</div>
</body>
</html>

在准备一个PDFUtil的工具类
PDFUtil工具类

public class PdfUtil {
    @Autowired
    private Configuration configuration;
    /**
     * 获取模板内容
     * @param templateDirectory 模板文件夹
     * @param templateName      模板文件名
     * @param paramMap          模板参数
     * @return
     * @throws Exception
     */
    public static String getTemplateContent(String templateDirectory, String templateName, Map<String, Object> paramMap) throws Exception {
        Configuration configuration = new Configuration(Configuration.DEFAULT_INCOMPATIBLE_IMPROVEMENTS);
        try {
            configuration.setDirectoryForTemplateLoading(new File(templateDirectory));
        } catch (Exception e) {
            System.out.println("-- exception --");
        }

        Writer out = new StringWriter();
        Template template = configuration.getTemplate(templateName,"UTF-8");
        template.process(paramMap, out);
        out.flush();
        out.close();
        return out.toString();
    }
    /**
     * HTML 转 PDF
     * @param content html内容
     * @param outPath           输出pdf路径
     * @return 是否创建成功
     */
    public static boolean html2Pdf(String content, String outPath) {
        try {
            ConverterProperties converterProperties = new ConverterProperties();
            converterProperties.setCharset("UTF-8");
            FontProvider fontProvider = new FontProvider();
            fontProvider.addSystemFonts();
            converterProperties.setFontProvider(fontProvider);
            HtmlConverter.convertToPdf(content, new FileOutputStream(outPath), converterProperties);
        } catch (Exception e) {
            log.error("生成模板内容失败,{}",e);
            return false;
        }
        return true;
    }
    /**
     * HTML 转 PDF
     * @param content html内容
     * @return PDF字节数组
     */
    public static byte[] html2Pdf(String content) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            ConverterProperties converterProperties = new ConverterProperties();
            converterProperties.setCharset("UTF-8");
            FontProvider fontProvider = new FontProvider();
            fontProvider.addSystemFonts();
            converterProperties.setFontProvider(fontProvider);
            HtmlConverter.convertToPdf(content,outputStream,converterProperties);
        } catch (Exception e) {
            log.error("生成 PDF 失败,{}",e);
        }
        return outputStream.toByteArray();
    }
}
Bean类
```java
@Data
public class PersonIntroduce {
    //名称
    private String personName ;
    //年龄
    private Integer personAge ;
    //性格描述
    private String personalityDesc;
    //性别
    private String personGender;
    //职业
    private String personVocation;
    //现居地址
    private String address;
    //创建时间
    private String createTime;
}

Controller层代码

@Controller
public class PersonIntroduceController {
    @Autowired
    private PersonIntroduceService personIntroduceService;	
    @GetMapping("/exPdf")
    @ResponseBody
    public void exPdfPersonIntroduce(HttpServletRequest request , HttpServletResponse response) throws TemplateException, IOException {
        PersonIntroduce personIntroduce = new PersonIntroduce();
        personIntroduce.setPersonName("小刘");
        personIntroduce.setAddress("北京朝阳区");
        personIntroduce.setPersonAge(24);
        personIntroduce.setPersonGender("男生");
        personIntroduce.setPersonalityDesc("其实我也不是很清楚");
        personIntroduce.setPersonVocation("Java后端开发");
        personIntroduceService.exPersonIntroduce(personIntroduce , request , response);
    }
}

Service业务层代码:
注意:建议使用这种方式,之前我在项目开发的过程中,使用了PdUtil.class.ClassLoader()这种方式去定位exPdf.html,在线下(开发环境)是可以使用的,但是部署到服务器的时候就出现了文件找不到的情况,因为上述这种方式他是使用的磁盘绝对路径查找的。使用freeMarkerConfigurer.getConfiguration().getTemplate(“exPdf.html”);来定位就不会出现这种问题。

@Service
public class PersonIntroduceServiceImpl implements PersonIntroduceService {
    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;
    /**
     * PDF导出类
     * @param personIntroduce
     */
    public void exPersonIntroduce(PersonIntroduce personIntroduce , HttpServletRequest  request, HttpServletResponse response) throws IOException, TemplateException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
        Map<String, Object> paramMap = new HashMap<>();
        personIntroduce.setCreateTime(sdf.format(new Date()));
        paramMap.put("person" , personIntroduce);
        Writer out = new StringWriter();
        //获取模板地址
        Template template = freeMarkerConfigurer.getConfiguration().getTemplate("exPdf.html");
        template.process(paramMap, out);
        out.flush();
        out.close();
        String templateContent = out.toString();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/pdf");
        String fileName =personIntroduce.getPersonName() + "-个人介绍-" + sdf.format(new Date());
        response.setHeader("Content-Disposition", "filename=" + new String(fileName.getBytes(), "iso8859-1"));

        byte[] resources = PdfUtil.html2Pdf(templateContent);
        ServletOutputStream outputStream = response.getOutputStream();
        outputStream.write(resources);
        outputStream.close();
    }
}

项目结构:
1680967754954.png

三、使用


1680967622137.png
这样就Ok了

Spring Boot项目中导出PDF文件是一个常见的需求,尤其是在需要生成报告、发票、合同等文档时。实现这一功能的方法有多种,具体取决于所使用的库以及需求的复杂程度。以下是一些常见的实现方法: ### 使用 iText 生成 PDF iText 是一个功能强大的 PDF 处理库,支持创建和修改 PDF 文件。在 Spring Boot 中,可以通过添加 iText 的依赖来使用它。iText 支持直接创建 PDF,也可以将 HTML 内容转换为 PDF。 ```xml <!-- iText 依赖 --> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.5.13</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> ``` 在代码中,可以通过 `Document` 类创建 PDF 文档,并通过 `PdfWriter` 将内容写入文件。此外,也可以使用 `XMLWorker` 或 `iText 7` 的 `pdfHTML` 模块将 HTML 内容转换为 PDF [^2]。 ### 使用 OpenPDF 生成 PDF OpenPDF 是 iText 的一个分支,提供了类似的 API 来生成 PDF 文件,并且是开源的。在 Spring Boot 中集成 OpenPDF 可以方便地将 HTML 或其他数据源转换为 PDF 格式。可以通过 Maven 添加 OpenPDF 依赖: ```xml <dependency> <groupId>org.danekja</groupId> <artifactId>java-ooxml-to-pdf</artifactId> <version>1.0.0</version> </dependency> ``` 通过 OpenPDF,可以将 HTML 模板渲染为 PDF,适用于需要动态生成文档内容的场景 [^1]。 ### 使用模板引擎 + PDF 转换工具 另一种方法是结合 Thymeleaf 或 Freemarker 等模板引擎生成 HTML 内容,然后使用工具如 Flying Saucer 或 OpenPDF 将 HTML 转换为 PDF。这种方法适合需要复杂样式和布局的 PDF 文档。 例如,可以使用 `ModelAndView` 将数据传递给 HTML 模板,然后通过自定义的 `PdfView` 实现导出逻辑。导出服务接口可以通过 Lambda 表达式实现,使得每个控制器都能自定义样式和数据 [^3]。 ### 示例代码:使用 iText 创建 PDF ```java import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.io.FileOutputStream; public class PdfExportExample { public void generatePdf(String filePath) throws Exception { Document document = new Document(); PdfWriter.getInstance(document, new FileOutputStream(filePath)); document.open(); document.add(new Paragraph("这是一个使用 iText 生成的 PDF 文件。")); document.close(); } } ``` ### 示例代码:使用 OpenPDF 将 HTML 转换为 PDF ```java import org.openpdf.text.Document; import org.openpdf.text.pdf.PdfWriter; import org.openpdf.tool.xml.XMLWorkerHelper; import java.io.FileOutputStream; import java.io.StringReader; public class HtmlToPdfExample { public void convertHtmlToPdf(String htmlContent, String filePath) throws Exception { Document document = new Document(); PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(filePath)); document.open(); XMLWorkerHelper.getInstance().parseXHtml(writer, document, new StringReader(htmlContent)); document.close(); } } ``` ### 总结 Spring Boot 导出 PDF 的实现方法多样,可以根据项目需求选择合适的库和工具。对于简单的 PDF 生产,iText 或 OpenPDF 是不错的选择;而对于需要复杂布局和样式的情况,结合模板引擎和 HTML 转 PDF 工具会更加灵活。 ---
评论 7
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值