• Spring Boot——Thymeleaf生成PDF实战教程



    前言

    温馨提示:本博客使用Thymeleaf模板引擎实现PDF打印仅供参考:

    在阅读该博客之前,先要了解一下Thymeleaf模板引擎,因为是使用Thymeleaf模板引擎实现的PDF打印的,

    Thymeleaf是一个现代的服务器端 Java 模板引擎,适用于 Web 和独立环境。

    Thymeleaf 的主要目标是为您的开发工作流程带来优雅的自然模板——HTML可以在浏览器中正确显示,也可以用作静态原型,从而在开发团队中实现更强大的协作。

    借助 Spring Framework 的模块、与您最喜欢的工具的大量集成以及插入您自己的功能的能力,Thymeleaf 是现代 HTML5 JVM Web 开发的理想选择——尽管它可以做的更多。
    不了解小伙伴可以去Thymeleaf官网查看,有更详细的讲解。
    接下来就不一一介绍了,直接上代码。


    一、引入依赖

    1.Thymeleaf,生成PDF相关依赖

    1.1,以下依赖为必要依赖,一个都不能少,依赖version可以根基实际情况使用相关的依赖版本。

    在这里插入图片描述

    二、application.yml配置

    1.yml配置文件

    yml配置文件使用配置thymeleaf模板路径(示例):
    在这里插入图片描述
    以上相关为基础且必须配置的内容,接下来继续讲解thymeleaf引擎需要生成PDF的相关配置。


    三、PDF相关配置

    1.PDF配置代码(如下):

    package com.cy.xgsm.configuration;
    
    import java.io.IOException;
    import java.io.InputStream;
    import java.net.URISyntaxException;
    
    import org.apache.commons.io.IOUtils;
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    
    import com.itextpdf.html2pdf.ConverterProperties;
    import com.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
    import com.itextpdf.io.font.PdfEncodings;
    import com.itextpdf.kernel.font.PdfFont;
    import com.itextpdf.kernel.font.PdfFontFactory;
    import com.itextpdf.layout.font.FontProvider;
    import com.cy.xgsm.controller.PrintPdfController;
    
    /**
     * 
     * @author Dylan
     * PDF相关配置
     */
    @Configuration
    public class PdfConfiguration {
    	
    	private static final Logger log = LoggerFactory.getLogger(PdfConfiguration.class);
    	
    	@Bean
    	public FontProvider getFontProvider() throws URISyntaxException, IOException {
    		FontProvider provider = new DefaultFontProvider(true, true, false);
    		byte[] bs = null;
    		//SIMSUN.TTC为字体
    		try (InputStream in = PrintPdfController.class.getClassLoader().getResourceAsStream("font/SIMSUN.TTC")) {
    			bs = IOUtils.toByteArray(in);
    		}		
    		PdfFont pdfFont = PdfFontFactory.createTtcFont(bs, 1, PdfEncodings.IDENTITY_H, false, true);
    		provider.addFont(pdfFont.getFontProgram());
    		return provider;
    	}
    	
    	@Bean
    	public ConverterProperties converterProperties(FontProvider fontProvider, Configuration config) {
    		ConverterProperties cp = new ConverterProperties();
    		cp.setBaseUri(config.getPdfUrl());
    		try {
    			cp.setFontProvider(fontProvider);
    		} catch (Exception e) {
    			log.error("打印PDF时未能添加字体", e);
    		}
    		return cp;
    	}
    	
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    一,注意PDF配置需要添加打印PDF字体,SIMSUN.TTC为打印需要的字体,但是也可以是其他的

    四、Controller

    1.以上所有的相关配置信息都配置完了,接下来就可以写Api接口了

    package com.cy.xgsm.controller;
    
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.URLEncoder;
    
    import javax.servlet.http.HttpServletResponse;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PathVariable;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.thymeleaf.TemplateEngine;
    import org.thymeleaf.context.Context;
    
    import com.itextpdf.html2pdf.ConverterProperties;
    import com.itextpdf.html2pdf.HtmlConverter;
    import com.itextpdf.kernel.geom.PageSize;
    import com.itextpdf.kernel.pdf.PdfDocument;
    import com.itextpdf.kernel.pdf.PdfWriter;
    import com.cy.xgsm.common.Result;
    import com.cy.xgsm.model.OrderInfo;
    import com.cy.xgsm.service.OrderInfoService;
    
    /**
     * 打印PDF 控制接入层
     * 
     * @author Dylan
     *
     */
    @Controller
    @RequestMapping("print")
    public class PrintPdfController {
    	
        private static final Logger log = LoggerFactory.getLogger(PrintPdfController.class);
        
    	@Autowired
    	private OrderInfoService service;
    	//thymeleaf模板引擎
        @Autowired
        TemplateEngine templateEngine;
    	//html转换成pdf需要使用ConverterProperties
        @Autowired
        ConverterProperties converterProperties;    
    	
        @GetMapping("order/{orderId}.pdf")
    	public void orderPdf(@PathVariable Long orderId, HttpServletResponse resp) throws IOException {
    		Result<OrderInfo> result = service.selectByPrimaryKey(orderId);
    		if (!result.isComplete()) {
                resp.sendError(404, "订单ID不存在");
            }
            Context context = new Context();
            context.setVariable("order", result.getData());
    		///html/pdf/order-template为打印模板纸张路径
            processPdf(context, "/html/pdf/order-template", result.getData().getKddh(), resp);
    		
    	}
    
    	/**
    	 * 调用生成PDF
    	 * @param context 上下文
    	 * @param template 模板文件
    	 * @param filename 文件名
    	 * @param resp
    	 */
    	private void processPdf(Context context, String template, String filename, HttpServletResponse resp) throws IOException {
            log.info("生成PDF:" + filename);
            String html = templateEngine.process(template, context);
            String filenameEncoded = URLEncoder.encode(filename, "utf-8");
            resp.setContentType("application/pdf");
            resp.setHeader("Content-Disposition", "filename=" + filenameEncoded + ".pdf");
            try (OutputStream out = resp.getOutputStream()) {
                PdfDocument doc = new PdfDocument(new PdfWriter(out));
    			//打印使用什么什么纸张可根据实际情况,我这里默认使用A4
                doc.setDefaultPageSize(PageSize.A4.rotate());
                HtmlConverter.convertToPdf(html, doc, converterProperties);
            }
    		
    	}
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85

    1.请求接口报错解决方式:

    如果在请求接口的时候发生以下错误信息是打印模板的路径错误了。
    在这里插入图片描述
    解决该错误需在你的yml配置thymeleaf路径即可,不懂怎么配置请往上看第二点application.yml配置,可按照application.yml复制上去即可解决。

    五、生成PDF文件响应效果

    在这里插入图片描述
    点击Save to a file保存,响应结果数据均为测试数据,仅供参考。
    在这里插入图片描述

  • 相关阅读:
    33. 搜索旋转排序数组
    6款好用良心的国产软件,每一款都是精品,电脑秒变黑科技
    Matlab:有序分类数组
    临床研究职业怎么应用RPA桌面端和移动端简单易用、快速上手
    Mac环境下反编译工具的使用
    Flink的API分层、架构与组件原理、并行度、任务执行计划
    【Python百日进阶-Web开发-Feffery】Day400 -“一起Dash”训练营Lesson-09_利用多页面应用_课堂
    android WindowManager的简单使用
    JVM年轻代(young generation)老年代(old generation tenured)持久代(permanent generation)GC
    学Spring5源码之入门
  • 原文地址:https://blog.csdn.net/weixin_45731661/article/details/126832804