java根據(jù)模板導(dǎo)出PDF的詳細(xì)實(shí)現(xiàn)過(guò)程
題記:
由于業(yè)務(wù)的需要,需要根據(jù)模板定制pdf文檔,經(jīng)測(cè)試根據(jù)模板導(dǎo)出word成功了;但是導(dǎo)出pdf相對(duì)麻煩了一點(diǎn)。兩天的研究測(cè)試java導(dǎo)出PDF,終于成功了,期間走了不少?gòu)澛罚穹窒沓鰜?lái),歡迎大家有問(wèn)題在此交流,與君共勉!
一、需求
根據(jù)業(yè)務(wù)需要,需要在服務(wù)器端生成可動(dòng)態(tài)配置的PDF文檔,方便數(shù)據(jù)可視化查看。
此文的測(cè)試是在客戶(hù)端通過(guò)java程序的測(cè)試,直接運(yùn)行java類(lèi)獲得成功!
二、解決方案
iText+FreeMarker+JFreeChart生成可動(dòng)態(tài)配置的PDF文檔。
iText有很強(qiáng)大的PDF處理能力,但是樣式和排版不好控制,直接寫(xiě)PDF文檔,數(shù)據(jù)的動(dòng)態(tài)渲染很麻煩。
FreeMarker能配置動(dòng)態(tài)的html模板,正好解決了樣式、動(dòng)態(tài)渲染和排版問(wèn)題。
JFreeChart有這方便的畫(huà)圖API,能畫(huà)出簡(jiǎn)單的折線(xiàn)、柱狀和餅圖,基本能滿(mǎn)足需要。
三、實(shí)現(xiàn)功能
1、能動(dòng)態(tài)配置PDF文檔內(nèi)容
2、能動(dòng)態(tài)配置中文字體顯示
3、設(shè)置自定義的頁(yè)眉頁(yè)腳信息
4、能動(dòng)態(tài)生成業(yè)務(wù)圖片
5、完成PDF的分頁(yè)和圖片的嵌入
四、主要代碼結(jié)構(gòu)說(shuō)明:
1、component包:PDF生成的組件 對(duì)外提供的是PDFKit工具類(lèi)和HeaderFooterBuilder接口,其中PDFKit負(fù)責(zé)PDF的生成,HeaderFooterBuilder負(fù)責(zé)自定義頁(yè)眉頁(yè)腳信息。
2、builder包:負(fù)責(zé)PDF模板之外的額外信息填寫(xiě),這里主要是頁(yè)眉頁(yè)腳的定制。
3、chart包:JFreeChart的畫(huà)圖工具包,目前只有一個(gè)線(xiàn)形圖。
4、test包:測(cè)試工具類(lèi)
5、util包:FreeMarker等工具類(lèi)。

項(xiàng)目采用maven架構(gòu),開(kāi)發(fā)工具為MyEclipse10,環(huán)境為jdk1.7
五、關(guān)鍵代碼說(shuō)明
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>
<!--第一頁(yè)開(kāi)始-->
<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>
<!--外部鏈接-->
<p>靜態(tài)logo圖</p>
<div>
<img src="${imageUrl}" alt="美團(tuán)點(diǎn)評(píng)" width="512" height="359"/>
</div>
<!--動(dòng)態(tài)生成的圖片-->
<p>氣溫變化對(duì)比圖</p>
<div>
<img src="${picUrl}" alt="我的圖片" width="500" height="270"/>
</div>
</div>
<!--第一頁(yè)結(jié)束-->
<!---分頁(yè)標(biāo)記-->
<span style="page-break-after:always;"></span>
<!--第二頁(yè)開(kāi)始-->
<div class="page">
<div>第二頁(yè)開(kāi)始了</div>
<div>列表值:</div>
<div>
<#list scores as item>
<div><p>${item}</p></div>
</#list>
</div>
</div>
<!--第二頁(yè)結(jié)束-->
</body>
</html>2、獲取模板內(nèi)容并填充數(shù)據(jù)
public static String getContent(String fileName,Object data){
String templatePath=getPDFTemplatePath(fileName).replace("\\", "/");
String templateFileName=getTemplateName(templatePath).replace("\\", "/");
String templateFilePath=getTemplatePath(templatePath).replace("\\", "/");
System.out.println("templatePath:"+templatePath);
System.out.println("templateFileName:"+templateFileName);
System.out.println("templateFilePath:"+templateFilePath);
if(StringUtils.isEmpty(templatePath)){
throw new FreeMarkerException("templatePath can not be empty!");
}
try{System.out.println("進(jìn)到這里了,有來(lái)無(wú)回1");
Configuration config = new Configuration(Configuration.VERSION_2_3_25);
config.setDefaultEncoding("UTF-8");
config.setDirectoryForTemplateLoading(new File(templateFilePath));
config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
config.setLogTemplateExceptions(false);System.out.println("進(jìn)到這里了,有來(lái)無(wú)回2");
Template template = config.getTemplate(templateFileName);System.out.println("進(jìn)到這里了,有來(lái)無(wú)回3");
StringWriter writer = new StringWriter();
template.process(data, writer);
writer.flush();
String html = writer.toString();
return html;
}catch (Exception ex){
throw new FreeMarkerException("FreeMarkerUtil process fail",ex);
}
}public static String getContent(String fileName,Object data){
String templatePath=getPDFTemplatePath(fileName);//根據(jù)PDF名稱(chēng)查找對(duì)應(yīng)的模板名稱(chē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ù)模板名稱(chēng) 獲取對(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){
try {
String htmlData= FreeMarkerUtil.getContent(fileName, data);
if(StringUtils.isEmpty(saveFilePath)){
saveFilePath=getDefaultSavePath(fileName);
}
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);
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
//設(shè)置頁(yè)眉頁(yè)腳
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);
}
} catch (Exception e) {
e.printStackTrace();
}
return saveFilePath;
}4、測(cè)試工具類(lèi)
public String createPDF(Object data, String fileName){
//pdf保存路徑
try {
//設(shè)置自定義PDF頁(yè)眉頁(yè)腳工具類(lèi)
PDFHeaderFooter headerFooter=new PDFHeaderFooter();
PDFKit kit=new PDFKit();
kit.setHeaderFooterBuilder(headerFooter);
//設(shè)置輸出路徑
kit.setSaveFilePath("D:/Users/hello.pdf");
String saveFilePath=kit.exportToFile(fileName,data);
return saveFilePath;
} catch (Exception e) {
System.out.println("竟然失敗了,艸!");
e.printStackTrace();
// log.error("PDF生成失敗{}", ExceptionUtils.getFullStackTrace(e));
log.error("PDF生成失敗{}");
return null;
}
}
public static void main(String[] args) {
ReportKit360 kit=new ReportKit360();
TemplateBO templateBO=new TemplateBO();
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("E:/圖片2/004d.jpg");
List<String> scores=new ArrayList<String>();
scores.add("94");
scores.add("95");
scores.add("98");
templateBO.setScores(scores);
List<Line> lineList=getTemperatureLineList();
DefaultLineChart lineChart=new DefaultLineChart();
lineChart.setHeight(500);
lineChart.setWidth(300);
String picUrl=lineChart.draw(lineList,0);
templateBO.setPicUrl(picUrl);System.out.println("picUrl:"+picUrl);
String path= kit.createPDF(templateBO,"hello.pdf");
System.out.println("打?。?+path);
}此測(cè)試工具類(lèi)中,要注意幾點(diǎn):
1)templateBO.setImageUrl("E:/圖片2/004d.jpg");中的參數(shù)修改為自己本地有的圖片;
2)程序可能會(huì)報(bào)找不到模板引擎hello.ftl文件的錯(cuò)誤,一定要將源碼中的hello.ftl放在本地硬盤(pán)對(duì)應(yīng)的目錄中;
六、生成效果圖

七、遇到的坑
1、FreeMarker配置模板文件樣式,在實(shí)際PDF生成過(guò)程中,可能會(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)目中配置了資源文件的過(guò)濾,不能識(shí)別二進(jìn)制文件導(dǎo)致的,plugins中增加下面這個(gè)配置就好了:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<!--增加的配置,過(guò)濾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分頁(yè)配置:
在ftl文件中,增加分頁(yè)標(biāo)簽: <span style="page-break-after:always;"></span>
八、項(xiàng)目說(shuō)明
此項(xiàng)目最初是由github上的開(kāi)源項(xiàng)目經(jīng)二次開(kāi)發(fā)而成,附github源碼地址:https://github.com/superad/pdf-kit;
但是github上的源碼bug太多,幾乎不能運(yùn)行,經(jīng)過(guò)一天的測(cè)試修改,才完全消除了它的bug;經(jīng)過(guò)測(cè)試已經(jīng)在windows系統(tǒng),jdk1.7,MyEclipse10中運(yùn)行成功;此項(xiàng)目只需要在MyEclipse中右擊ReportKit360.java文件,然后選擇run as java application即可,如圖:

下面是整合到web網(wǎng)站中,在網(wǎng)頁(yè)中填充內(nèi)容,然后自動(dòng)生成pdf文檔后在網(wǎng)頁(yè)端查看或者下載。
九、整合到web項(xiàng)目中遇到的坑
1、讀取的模板.ftl文檔時(shí),

發(fā)現(xiàn)讀取的內(nèi)容htmlData開(kāi)始多了一個(gè)?,幾經(jīng)搜索后發(fā)現(xiàn)是因?yàn)槲臋n編碼格式的原因,于是在editplus中將其打開(kāi)并重新另存為無(wú)bom格式的文檔后重新讀取,發(fā)現(xiàn)?消失了。
雖然解決了讀取的問(wèn)題,但是還是沒(méi)有解決下載pdf亂碼的問(wèn)題。
2、又重新debug項(xiàng)目之后發(fā)現(xiàn),不是字體讀取的問(wèn)題,因?yàn)槲募A下的字體是能夠讀取到的,于是懷疑是編碼問(wèn)題,將所有編碼修改為UTF-8格式,仍沒(méi)有解決亂碼問(wèn)題,又繼續(xù)debug項(xiàng)目,幾經(jīng)細(xì)致查看后,感覺(jué)應(yīng)該是文件讀取時(shí)是在web容器中的,這一步編碼不太容易修改,于是決定按照讀取是什么編碼就改為什么編碼,最終獲得成功。
web項(xiàng)目代碼結(jié)構(gòu)如下:

啟動(dòng)服務(wù)器后,在瀏覽器中輸入http://localhost:8080/項(xiàng)目名/index.action后回車(chē),即可進(jìn)入前端輸入pdf文檔內(nèi)容的頁(yè)面,輸入完成后點(diǎn)擊提交,即可下載pdf文檔,生成的文檔格式完全正確,并且沒(méi)有亂碼。
參考文章:http://m.fzitv.net/article/112366.htm
總結(jié)
到此這篇關(guān)于java根據(jù)模板導(dǎo)出PDF的文章就介紹到這了,更多相關(guān)java根據(jù)模板導(dǎo)出PDF內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java調(diào)用通義千問(wèn)API的詳細(xì)完整步驟
通義千問(wèn)是阿里云自主研發(fā)的大語(yǔ)言模型,能夠在用戶(hù)自然語(yǔ)言輸入的基礎(chǔ)上,通過(guò)自然語(yǔ)言理解和語(yǔ)義分析,理解用戶(hù)意圖,在不同領(lǐng)域、任務(wù)內(nèi)為用戶(hù)提供服務(wù)和幫助,下面這篇文章主要給大家介紹了關(guān)于java調(diào)用通義千問(wèn)API的詳細(xì)完整步驟,需要的朋友可以參考下2024-02-02
Springboot+TCP監(jiān)聽(tīng)服務(wù)器搭建過(guò)程圖解
這篇文章主要介紹了Springboot+TCP監(jiān)聽(tīng)服務(wù)器搭建過(guò)程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-10-10
jpa多數(shù)據(jù)源時(shí)Hibernate配置自動(dòng)生成表不生效的解決
這篇文章主要介紹了jpa多數(shù)據(jù)源時(shí)Hibernate配置自動(dòng)生成表不生效的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring Web MVC和Hibernate的集成配置詳解
這篇文章主要介紹了Spring Web MVC和Hibernate的集成配置詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下2017-12-12
Springboot配置文件內(nèi)容加密代碼實(shí)例
這篇文章主要介紹了Springboot配置文件內(nèi)容加密代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
java中Callback簡(jiǎn)單使用總結(jié)
正好學(xué)習(xí)到j(luò)ava Callback,就整理了一下,希望整理的文章內(nèi)容對(duì)大家有所幫助2017-04-04
maven setting多倉(cāng)庫(kù)配置方式
這篇文章主要介紹了maven setting多倉(cāng)庫(kù)配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05
解決idea update project 更新選項(xiàng)消失的問(wèn)題
這篇文章主要介紹了解決idea update project 更新選項(xiàng)消失的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
詳解Spring AOP 實(shí)現(xiàn)主從讀寫(xiě)分離
本篇文章主要介紹了Spring AOP 實(shí)現(xiàn)主從讀寫(xiě)分離,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-03-03
java動(dòng)態(tài)綁定和靜態(tài)綁定用法實(shí)例詳解
這篇文章主要介紹了java動(dòng)態(tài)綁定和靜態(tài)綁定用法,結(jié)合實(shí)例形式詳細(xì)分析了java動(dòng)態(tài)綁定與靜態(tài)綁定相關(guān)概念、原理、實(shí)現(xiàn)方法及使用注意事項(xiàng),需要的朋友可以參考下2019-05-05

