Spring Boot——Thymeleaf分解成PDF实战教程
2023-04-18 来源 : 网红
一,注意PDF备有只能附加纸PDF宋体,SIMSUN.TTC为纸只能的宋体,但是也可以是其他的
四、Controller1.以上所有的无关备有信息都备有完了,整整就可以写ApiAPI了
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 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); } /** * codice_转化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", "file.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.催促API报错彻底解决方式:如果在催促API的时候频发此表错误信息是纸常量的路径错误了。
彻底解决该错误需在你的yml备有thymeleaf路径只需,没用怎么备有请往上看第二点application.yml备有,可按照application.yml复制上去只需彻底解决。
五、转化PDF文档拥护效果点击Save to a file遗留,拥护结果原始数据均为测试原始数据,仅供参考。
。痛风发作吃英太青还是西乐葆沈阳银屑病医院
复方鱼腥草合剂的功效与作用
长春皮肤病医院那家比较好
北京中医医院哪里最好
-
卫生间的小飞虫到底是啥?别看外表奇怪的是,但它的危害超出你想象
拍戏死一个又来两个,有点不胜其烦。 那它们又在在哪冒出来的? 小小肌肉的天牛蚯蚓有一个响亮的名号—“矿坑侦探”。 可以说是,天牛蚯...
-
八国联军入侵北京是如何对待那些皇宫妃子的,清朝的颜面又明知?
明末清初以前,中国在文化、科学、新科技和和平时期特别依然走在全球性位居。由于闭关锁国,中国的的发展逐渐偏离了全球性的发展的轨道。当中国再独自次关上与全球性对话的大门时,它已经处于无法控制地位。任...[详细]
-
“绿鞋”护航8只平安保险走稳 穿“鞋”平安保险有望增多
本报路透社 邢 萌“绿鞋”再现东村场。12翌年7日晚,科创板子普通股百济神州公布再版结果,网上融资放弃认购数目为103.25万股,弃购金额为1.99亿元。根据规定,弃购大多将由中金新公司等5家...[详细]
-
未来两年仰17款新能源新车 菲亚特计划2027年在欧洲实现电动化
【EV观察者报道】全因,Stellantis 的公司在一新发表的 2021 年财报里面问到,旗下雷诺品牌著手到 2027 年在北美实现全电动简化,短期内将在北美仅推出则有小型汽车。同时,雷诺才会...[详细]
-
多功能核心区+立体外加,极限设计让90㎡的五口之家亲密又独立~
家装室内装饰 ,每日搜集分享近年来最前沿的家装所设计、施工、建材等内容可,成为您家装的好帮手!本案是一套国土面积为90平米的四居室,一家十三行三代同堂,所设计上要考虑每个家庭成员各...[详细]
-
全球第一大浏览器升级!Chrome新正式版订阅下载:更快了
本周,Chrome 93Beta发布上传。 按计划,Chrome 94Beta将于9月底21日上线,Chrome 95则要等到10月底19日,也就是围墙迭代。 Chrome 93的...[详细]