java根據(jù)模板動(dòng)態(tài)生成PDF實(shí)例
一、需求說明:
根據(jù)業(yè)務(wù)需要,需要在服務(wù)器端生成可動(dòng)態(tài)配置的PDF文檔,方便數(shù)據(jù)可視化查看。
二、解決方案:
iText+FreeMarker+JFreeChart生成可動(dòng)態(tài)配置的PDF文檔
iText有很強(qiáng)大的PDF處理能力,但是樣式和排版不好控制,直接寫PDF文檔,數(shù)據(jù)的動(dòng)態(tài)渲染很麻煩。
FreeMarker能配置動(dòng)態(tài)的html模板,正好解決了樣式、動(dòng)態(tài)渲染和排版問題。
JFreeChart有這方便的畫圖API,能畫出簡(jiǎn)單的折線、柱狀和餅圖,基本能滿足需要。
三、實(shí)現(xiàn)功能:
1、能動(dòng)態(tài)配置PDF文檔內(nèi)容
2、能動(dòng)態(tài)配置中文字體顯示
3、設(shè)置自定義的頁眉頁腳信息
4、能動(dòng)態(tài)生成業(yè)務(wù)圖片
5、完成PDF的分頁和圖片的嵌入
四、主要代碼結(jié)構(gòu)說明:
1、component包:PDF生成的組件 對(duì)外提供的是PDFKit工具類和HeaderFooterBuilder接口,其中PDFKit負(fù)責(zé)PDF的生成,HeaderFooterBuilder負(fù)責(zé)自定義頁眉頁腳信息。
2、builder包:負(fù)責(zé)PDF模板之外的額外信息填寫,這里主要是頁眉頁腳的定制。
3、chart包:JFreeChart的畫圖工具包,目前只有一個(gè)線形圖。
4、test包:測(cè)試工具類
5、util包:FreeMarker等工具類。
五、關(guān)鍵代碼說明:
1、模板配置
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <meta http-equiv="Content-Style-Type" content="text/css"/> <title></title> <style type="text/css"> body { font-family: pingfang sc light; } .center{ text-align: center; width: 100%; } </style> </head> <body> <!--第一頁開始--> <div class="page" > <div class="center"><p>${templateName}</p></div> <div><p>iText官網(wǎng):${ITEXTUrl}</p></div> <div><p>FreeMarker官網(wǎng):${freeMarkerUrl}</p></div> <div><p>JFreeChart教程:${JFreeChartUrl}</p></div> <div>列表值:</div> <div> <#list scores as item> <div><p>${item}</p></div> </#list> </div> </div> <!--第一頁結(jié)束--> <!---分頁標(biāo)記--> <span style="page-break-after:always;"></span> <!--第二頁開始--> <div class="page"> <div>第二頁開始了</div> <!--外部鏈接--> <p>百度圖標(biāo)</p> <div> <img src="${imageUrl}" alt="百度圖標(biāo)" width="270" height="129"/> </div> <!--動(dòng)態(tài)生成的圖片--> <p>氣溫變化對(duì)比圖</p> <div> <img src="${picUrl}" alt="我的圖片" width="500" height="270"/> </div> </div> <!--第二頁結(jié)束--> </body> </html>
2、獲取模板內(nèi)容并填充數(shù)據(jù)
/** * @description 獲取模板 */ public static String getContent(String fileName,Object data){ String templatePath=getPDFTemplatePath(fileName);//根據(jù)PDF名稱查找對(duì)應(yīng)的模板名稱 String templateFileName=getTemplateName(templatePath); String templateFilePath=getTemplatePath(templatePath); if(StringUtils.isEmpty(templatePath)){ throw new FreeMarkerException("templatePath can not be empty!"); } try{ Configuration config = new Configuration(Configuration.VERSION_2_3_25);//FreeMarker配置 config.setDefaultEncoding("UTF-8"); config.setDirectoryForTemplateLoading(new File(templateFilePath));//注意這里是模板所在文件夾,不是文件 config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER); config.setLogTemplateExceptions(false); Template template = config.getTemplate(templateFileName);//根據(jù)模板名稱 獲取對(duì)應(yīng)模板 StringWriter writer = new StringWriter(); template.process(data, writer);//模板和數(shù)據(jù)的匹配 writer.flush(); String html = writer.toString(); return html; }catch (Exception ex){ throw new FreeMarkerException("FreeMarkerUtil process fail",ex); } }
3、導(dǎo)出模板到PDF文件
/** * @description 導(dǎo)出pdf到文件 * @param fileName 輸出PDF文件名 * @param data 模板所需要的數(shù)據(jù) * */ public String exportToFile(String fileName,Object data){ String htmlData= FreeMarkerUtil.getContent(fileName, data);//獲取FreeMarker的模板數(shù)據(jù) if(StringUtils.isEmpty(saveFilePath)){ saveFilePath=getDefaultSavePath(fileName);//設(shè)置PDF文件輸出路徑 } File file=new File(saveFilePath); if(!file.getParentFile().exists()){ file.getParentFile().mkdirs(); } FileOutputStream outputStream=null; try{ //設(shè)置輸出路徑 outputStream=new FileOutputStream(saveFilePath); //設(shè)置文檔大小 Document document = new Document(PageSize.A4);//IText新建PDF文檔 PdfWriter writer = PdfWriter.getInstance(document, outputStream);//設(shè)置文檔和輸出流的關(guān)系 //設(shè)置頁眉頁腳 PDFBuilder builder = new PDFBuilder(headerFooterBuilder,data); builder.setPresentFontSize(10); writer.setPageEvent(builder); //輸出為PDF文件 convertToPDF(writer,document,htmlData); }catch(Exception ex){ throw new PDFException("PDF export to File fail",ex); }finally{ IOUtils.closeQuietly(outputStream); } return saveFilePath; }
4、測(cè)試工具類
public String createPDF(Object data, String fileName){ //pdf保存路徑 try { //設(shè)置自定義PDF頁眉頁腳工具類 PDFHeaderFooter headerFooter=new PDFHeaderFooter(); PDFKit kit=new PDFKit(); kit.setHeaderFooterBuilder(headerFooter); //設(shè)置輸出路徑 kit.setSaveFilePath("/Users/fgm/Desktop/pdf/hello.pdf”);//設(shè)置出書路徑 String saveFilePath=kit.exportToFile(fileName,data); return saveFilePath; } catch (Exception e) { log.error("PDF生成失敗{}", ExceptionUtils.getFullStackTrace(e)); return null; } }
public static void main(String[] args) { ReportKit360 kit=new ReportKit360(); TemplateBO templateBO=new TemplateBO();//配置模板數(shù)據(jù) templateBO.setTemplateName("Hello iText! Hello freemarker! Hello jFreeChart!"); templateBO.setFreeMarkerUrl("http://www.zheng-hang.com/chm/freemarker2_3_24/ref_directive_if.html"); templateBO.setITEXTUrl("http://developers.itextpdf.com/examples-itext5"); templateBO.setJFreeChartUrl("http://www.yiibai.com/jfreechart/jfreechart_referenced_apis.html"); templateBO.setImageUrl("https://ss0.bdstatic.com/5aV1bjqh_Q23odCf/static/superman/img/logo/bd_logo1_31bdc765.png"); List<String> scores=new ArrayList<String>(); scores.add("90"); scores.add("95"); scores.add("98"); templateBO.setScores(scores); List<Line> lineList=getTemperatureLineList(); TemperatureLineChart lineChart=new TemperatureLineChart(); String picUrl=lineChart.draw(lineList,0);//自定義的數(shù)據(jù)畫圖 templateBO.setPicUrl(picUrl); String path= kit.createPDF(templateBO,"hello.pdf"); System.out.println(path); }
六、生成效果圖:
七、項(xiàng)目完整代碼
1、github地址:https://github.com/superad/pdf-kit
八、遇到的坑:
1、FreeMarker配置模板文件樣式,在實(shí)際PDF生成過程中,可能會(huì)出現(xiàn)一些不一致的情形,目前解決方法,就是換種方式調(diào)整樣式。
2、字體文件放在resource下,在打包時(shí)會(huì)報(bào)錯(cuò),運(yùn)行mvn -X compile 會(huì)看到詳細(xì)錯(cuò)誤:
這是字體文件是二進(jìn)制的,而maven項(xiàng)目中配置了資源文件的過濾,不能識(shí)別二進(jìn)制文件導(dǎo)致的,plugins中增加下面這個(gè)配置就好了:
<build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> <!--增加的配置,過濾ttf文件的匹配--> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>2.7</version> <configuration> <encoding>UTF-8</encoding> <nonFilteredFileExtensions> <nonFilteredFileExtension>ttf</nonFilteredFileExtension> </nonFilteredFileExtensions> </configuration> </plugin> </plugins> </build>
3、PDF分頁配置:
在ftl文件中,增加分頁標(biāo)簽: <span style="page-break-after:always;"></span>
九、 完整maven配置:
<!--pdf生成 itext--> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itextpdf</artifactId> <version>5.4.2</version> </dependency> <dependency> <groupId>com.itextpdf.tool</groupId> <artifactId>xmlworker</artifactId> <version>5.4.1</version> </dependency> <dependency> <groupId>com.itextpdf</groupId> <artifactId>itext-asian</artifactId> <version>5.2.0</version> </dependency> <dependency> <groupId>org.xhtmlrenderer</groupId> <artifactId>flying-saucer-pdf</artifactId> <version>9.0.3</version> </dependency> <!--freemarker--> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.26-incubating</version> </dependency> <!--jfreechart--> <dependency> <groupId>jfreechart</groupId> <artifactId>jfreechart</artifactId> <version>1.0.0</version> </dependency> <!--log--> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-core</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-classic</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>ch.qos.logback</groupId> <artifactId>logback-access</artifactId> <version>1.0.13</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>log4j-over-slf4j</artifactId> <version>1.7.21</version> </dependency> <!--util--> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>20.0</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.14.8</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-io</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>commons-lang</groupId> <artifactId>commons-lang</artifactId> <version>2.6</version> </dependency> <!--servlet--> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java Mybatis架構(gòu)設(shè)計(jì)深入了解
在本篇文章里小編給大家整理的是一篇關(guān)于Java Mybatis架構(gòu)設(shè)計(jì)詳解內(nèi)容,對(duì)此有興趣的朋友們可以參考下,希望能夠給你帶來幫助2021-11-11SpringBoot中將@Bean方法解析為BeanDefinition詳解
這篇文章主要介紹了SpringBoot中將@Bean方法解析為BeanDefinition詳解,得到的BeanDefinition是ConfigurationClassBeanDefinition類型,會(huì)為BeanDefinition設(shè)置factoryMethodName,這意味著當(dāng)實(shí)例化這個(gè)bean的時(shí)候?qū)⒉捎霉S方法,需要的朋友可以參考下2023-12-12金三銀四復(fù)工高頻面試題java算法LeetCode396旋轉(zhuǎn)函數(shù)
這篇文章主要為大家介紹了金三銀四復(fù)工高頻面試題之java算法題解LeetCode396旋轉(zhuǎn)函數(shù),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02Spring?Cloud詳細(xì)講解zuul集成Eureka流程
這篇文章主要介紹了Spring?Cloud?zuul集成Eureka,Eureka?Client中內(nèi)置一個(gè)負(fù)載均衡器,用來進(jìn)行基本的負(fù)載均衡,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06