SpringBoot+Thymeleaf實現(xiàn)生成PDF文檔
前言
溫馨提示:本博客使用Thymeleaf模板引擎實現(xiàn)PDF打印僅供參考:
在閱讀該博客之前,先要了解一下Thymeleaf模板引擎,因為是使用Thymeleaf模板引擎實現(xiàn)的PDF打印的,
Thymeleaf是一個現(xiàn)代的服務(wù)器端 Java 模板引擎,適用于 Web 和獨立環(huán)境。
Thymeleaf 的主要目標是為您的開發(fā)工作流程帶來優(yōu)雅的自然模板——HTML可以在瀏覽器中正確顯示,也可以用作靜態(tài)原型,從而在開發(fā)團隊中實現(xiàn)更強大的協(xié)作。
借助 Spring Framework 的模塊、與您最喜歡的工具的大量集成以及插入您自己的功能的能力,Thymeleaf 是現(xiàn)代 HTML5 JVM Web 開發(fā)的理想選擇——盡管它可以做的更多。
不了解小伙伴可以去Thymeleaf官網(wǎng)查看,有更詳細的講解。
接下來就不一一介紹了,直接上代碼。
一、引入依賴
1.Thymeleaf,生成PDF相關(guān)依賴
1.1 以下依賴為必要依賴,一個都不能少,依賴version可以根基實際情況使用相關(guān)的依賴版本。
二、application.yml配置
1.yml配置文件
yml配置文件使用配置thymeleaf模板路徑(示例):
以上相關(guān)為基礎(chǔ)且必須配置的內(nèi)容,接下來繼續(xù)講解thymeleaf引擎需要生成PDF的相關(guān)配置。
三、PDF相關(guān)配置
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相關(guān)配置 */ @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; } }
注意PDF配置需要添加打印PDF字體,SIMSUN.TTC為打印需要的字體,但是也可以是其他的
四、Controller
1.以上所有的相關(guān)配置信息都配置完了,接下來就可以寫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轉(zhuǎn)換成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); } /** * 調(diào)用生成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)); //打印使用什么什么紙張可根據(jù)實際情況,我這里默認使用A4 doc.setDefaultPageSize(PageSize.A4.rotate()); HtmlConverter.convertToPdf(html, doc, converterProperties); } } }
1.請求接口報錯解決方式:
如果在請求接口的時候發(fā)生以下錯誤信息是打印模板的路徑錯誤了。
解決該錯誤需在你的yml配置thymeleaf路徑即可,不懂怎么配置請往上看第二點application.yml配置,可按照application.yml復(fù)制上去即可解決。
五、生成PDF文件響應(yīng)效果
點擊Save to a file保存,響應(yīng)結(jié)果數(shù)據(jù)均為測試數(shù)據(jù),僅供參考。
到此這篇關(guān)于SpringBoot+Thymeleaf實現(xiàn)生成PDF文檔的文章就介紹到這了,更多相關(guān)SpringBoot Thymeleaf生成PDF內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java 數(shù)據(jù)結(jié)構(gòu)并查集詳解
并查集是一種用來管理元素分組情況的數(shù)據(jù)結(jié)構(gòu)。并查集可以高效地進行如下操作。本文將通過Java實現(xiàn)并查集,感興趣的小伙伴可以了解一下2022-03-03java Nio使用NioSocket客戶端與服務(wù)端交互實現(xiàn)方式
這篇文章主要介紹了java Nio使用 NioSocket 客戶端與服務(wù)端交互實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-06-06spring boot如何基于JWT實現(xiàn)單點登錄詳解
這篇文章主要介紹了spring boot如何基于JWT實現(xiàn)單點登錄詳解,用戶只需登錄一次就能夠在這兩個系統(tǒng)中進行操作。很明顯這就是單點登錄(Single Sign-On)達到的效果,需要的朋友可以參考下2019-06-06Spring Cloud Alibaba Nacos Config進階使用
這篇文章主要介紹了Spring Cloud Alibaba Nacos Config進階使用,文中使用企業(yè)案例,圖文并茂的展示了Nacos Config的使用,感興趣的小伙伴可以看一看2021-08-08Java集合框架之List ArrayList LinkedList使用詳解刨析
早在 Java 2 中之前,Java 就提供了特設(shè)類。比如:Dictionary, Vector, Stack, 和 Properties 這些類用來存儲和操作對象組。雖然這些類都非常有用,但是它們?nèi)鄙僖粋€核心的,統(tǒng)一的主題。由于這個原因,使用 Vector 類的方式和使用 Properties 類的方式有著很大不同2021-10-10