版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
第SpringBoot+Thymeleaf實(shí)現(xiàn)生成PDF文檔目錄前言一、引入依賴二、application.yml配置三、PDF相關(guān)配置四、Controller五、生成PDF文件響應(yīng)效果
前言
溫馨提示:本博客使用Thymeleaf模板引擎實(shí)現(xiàn)PDF打印僅供參考:
在閱讀該博客之前,先要了解一下Thymeleaf模板引擎,因?yàn)槭鞘褂肨hymeleaf模板引擎實(shí)現(xiàn)的PDF打印的,
Thymeleaf是一個(gè)現(xiàn)代的服務(wù)器端Java模板引擎,適用于Web和獨(dú)立環(huán)境。
Thymeleaf的主要目標(biāo)是為您的開(kāi)發(fā)工作流程帶來(lái)優(yōu)雅的自然模板HTML可以在瀏覽器中正確顯示,也可以用作靜態(tài)原型,從而在開(kāi)發(fā)團(tuán)隊(duì)中實(shí)現(xiàn)更強(qiáng)大的協(xié)作。
借助SpringFramework的模塊、與您最喜歡的工具的大量集成以及插入您自己的功能的能力,Thymeleaf是現(xiàn)代HTML5JVMWeb開(kāi)發(fā)的理想選擇盡管它可以做的更多。
不了解小伙伴可以去Thymeleaf官網(wǎng)查看,有更詳細(xì)的講解。
接下來(lái)就不一一介紹了,直接上代碼。
一、引入依賴
1.Thymeleaf,生成PDF相關(guān)依賴
1.1以下依賴為必要依賴,一個(gè)都不能少,依賴version可以根基實(shí)際情況使用相關(guān)的依賴版本。
二、application.yml配置
1.yml配置文件
yml配置文件使用配置thymeleaf模板路徑(示例):
以上相關(guān)為基礎(chǔ)且必須配置的內(nèi)容,接下來(lái)繼續(xù)講解thymeleaf引擎需要生成PDF的相關(guān)配置。
三、PDF相關(guān)配置
1.PDF配置代碼(如下):
packagecom.cy.xgsm.configuration;
importjava.io.IOException;
importjava.io.InputStream;
import.URISyntaxException;
importmons.io.IOUtils;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importcom.itextpdf.html2pdf.ConverterProperties;
importcom.itextpdf.html2pdf.resolver.font.DefaultFontProvider;
importcom.itextpdf.io.font.PdfEncodings;
importcom.itextpdf.kernel.font.PdfFont;
importcom.itextpdf.kernel.font.PdfFontFactory;
importcom.itextpdf.layout.font.FontProvider;
importcom.cy.xgsm.controller.PrintPdfController;
*@authorDylan
*PDF相關(guān)配置
@Configuration
publicclassPdfConfiguration{
privatestaticfinalLoggerlog=LoggerFactory.getLogger(PdfConfiguration.class);
@Bean
publicFontProvidergetFontProvider()throwsURISyntaxException,IOException{
FontProviderprovider=newDefaultFontProvider(true,true,false);
byte[]bs=null;
//SIMSUN.TTC為字體
try(InputStreamin=PrintPdfController.class.getClassLoader().getResourceAsStream("font/SIMSUN.TTC")){
bs=IOUtils.toByteArray(in);
PdfFontpdfFont=PdfFontFactory.createTtcFont(bs,1,PdfEncodings.IDENTITY_H,false,true);
provider.addFont(pdfFont.getFontProgram());
returnprovider;
@Bean
publicConverterPropertiesconverterProperties(FontProviderfontProvider,Configurationconfig){
ConverterPropertiescp=newConverterProperties();
cp.setBaseUri(config.getPdfUrl());
try{
cp.setFontProvider(fontProvider);
}catch(Exceptione){
log.error("打印PDF時(shí)未能添加字體",e);
returncp;
注意PDF配置需要添加打印PDF字體,SIMSUN.TTC為打印需要的字體,但是也可以是其他的
四、Controller
1.以上所有的相關(guān)配置信息都配置完了,接下來(lái)就可以寫(xiě)Api接口了
packagecom.cy.xgsm.controller;
importjava.io.IOException;
importjava.io.OutputStream;
import.URLEncoder;
importjavax.servlet.http.HttpServletResponse;
importorg.slf4j.Logger;
importorg.slf4j.LoggerFactory;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Controller;
importorg.springframework.web.bind.annotation.GetMapping;
importorg.springframework.web.bind.annotation.PathVariable;
importorg.springframework.web.bind.annotation.RequestMapping;
importorg.thymeleaf.TemplateEngine;
importorg.thymeleaf.context.Context;
importcom.itextpdf.html2pdf.ConverterProperties;
importcom.itextpdf.html2pdf.HtmlConverter;
importcom.itextpdf.kernel.geom.PageSize;
importcom.itextpdf.kernel.pdf.PdfDocument;
importcom.itextpdf.kernel.pdf.PdfWriter;
importmon.Result;
importcom.cy.xgsm.model.OrderInfo;
importcom.cy.xgsm.service.OrderInfoService;
*打印PDF控制接入層
*@authorDylan
@Controller
@RequestMapping("print")
publicclassPrintPdfController{
privatestaticfinalLoggerlog=LoggerFactory.getLogger(PrintPdfController.class);
@Autowired
privateOrderInfoServiceservice;
//thymeleaf模板引擎
@Autowired
TemplateEnginetemplateEngine;
//html轉(zhuǎn)換成pdf需要使用ConverterProperties
@Autowired
ConverterPropertiesconverterProperties;
@GetMapping("order/{orderId}.pdf")
publicvoidorderPdf(@PathVariableLongorderId,HttpServletResponseresp)throwsIOException{
ResultOrderInforesult=service.selectByPrimaryKey(orderId);
if(!result.isComplete()){
resp.sendError(404,"訂單ID不存在");
Contextcontext=newContext();
context.setVariable("order",result.getData());
///html/pdf/order-template為打印模板紙張路徑
processPdf(context,"/html/pdf/order-template",result.getData().getKddh(),resp);
*調(diào)用生成PDF
*@paramcontext上下文
*@paramtemplate模板文件
*@paramfilename文件名
*@paramresp
privatevoidprocessPdf(Contextcontext,Stringtemplate,Stringfilename,HttpServletResponseresp)throwsIOException{
("生成PDF:"+filename);
Stringhtml=templateEcess(template,context);
StringfilenameEncoded=URLEncoder.encode(filename,"utf-8");
resp.setContentType("application/pdf");
resp.setHeader("Content-Disposition","filename="+filenameEncoded+".pdf");
try(OutputStreamout=resp.getOutputStream()){
PdfDocumentdoc=newPdfDocument(newPdfWriter(out));
//打印使用什么什么紙張可根據(jù)實(shí)際情況,我這里默認(rèn)使用A4
doc.setDefaultPageSize(PageSize.A4.rotate());
HtmlConverte
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026青海西寧城西區(qū)西部礦業(yè)集團(tuán)有限公司黨務(wù)工作部門(mén)業(yè)務(wù)崗位選聘5人參考考試試題附答案解析
- 酒廠生產(chǎn)廠長(zhǎng)規(guī)章制度
- 2026湖南長(zhǎng)沙市雨花區(qū)中雅培粹雙語(yǔ)中學(xué)合同制教師招聘參考考試題庫(kù)附答案解析
- 生產(chǎn)體系交付管理制度
- 生產(chǎn)企業(yè)招聘制度
- 2026甘肅白銀市平川區(qū)容通水務(wù)有限公司招聘9人備考考試題庫(kù)附答案解析
- 生產(chǎn)車間雙面膠管理制度
- 2026河南鄭州管城回族區(qū)嵩陽(yáng)學(xué)校(小學(xué)部)招聘3人參考考試題庫(kù)附答案解析
- 造紙廠鍋爐安全生產(chǎn)制度
- 安全生產(chǎn)指揮官制度
- 2026年高考語(yǔ)文復(fù)習(xí)散文閱讀(四)
- 眼部艾灸課件
- 學(xué)堂在線 雨課堂 學(xué)堂云 實(shí)繩結(jié)技術(shù) 期末考試答案
- 乘務(wù)長(zhǎng)崗位面試技巧與實(shí)戰(zhàn)經(jīng)驗(yàn)分享
- 氣道濕化方法與指南
- 國(guó)家電力安全生產(chǎn)課件
- 2025年四川公務(wù)員面試真題及答案
- 安裝吊扇施工方案
- 分紅、年金、萬(wàn)能保險(xiǎn)測(cè)試題附答案
- GB/T 46456.3-2025信息技術(shù)設(shè)備互連智能家居互聯(lián)互通第3部分:局域互聯(lián)通用要求
- 國(guó)家基層高血壓防治管理指南 2025版圖文解讀
評(píng)論
0/150
提交評(píng)論