原文:https://blog.csdn.net/pengyufight/article/details/75305128
題記:由於業務的需要,需要根據模板定制pdf文檔,經測試根據模板導出word成功了;但是導出pdf相對麻煩了一點。兩天的研究測試java導出PDF,終於成功了,期間走了不少彎路,今分享出來,歡迎大家有問題在此交流,與君共勉!
一、需求
根據業務需要,需要在服務器端生成可動態配置的PDF文檔,方便數據可視化查看。
此文的測試是在客戶端通過java程序的測試,直接運行java類獲得成功!
二、解決方案
iText+FreeMarker+JFreeChart生成可動態配置的PDF文檔。
iText有很強大的PDF處理能力,但是樣式和排版不好控制,直接寫PDF文檔,數據的動態渲染很麻煩。
FreeMarker能配置動態的html模板,正好解決了樣式、動態渲染和排版問題。
JFreeChart有這方便的畫圖API,能畫出簡單的折線、柱狀和餅圖,基本能滿足需要。
三、實現功能
1、能動態配置PDF文檔內容
2、能動態配置中文字體顯示
3、設置自定義的頁眉頁腳信息
4、能動態生成業務圖片
5、完成PDF的分頁和圖片的嵌入
四、主要代碼結構說明:
1、component包:PDF生成的組件 對外提供的是PDFKit工具類和HeaderFooterBuilder接口,其中PDFKit負責PDF的生成,HeaderFooterBuilder負責自定義頁眉頁腳信息。
2、builder包:負責PDF模板之外的額外信息填寫,這里主要是頁眉頁腳的定制。
3、chart包:JFreeChart的畫圖工具包,目前只有一個線形圖。
4、test包:測試工具類
5、util包:FreeMarker等工具類。
項目采用maven架構,開發工具為MyEclipse10,環境為jdk1.7
五、關鍵代碼說明
1、模板配置
-
-
<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官網:${ITEXTUrl}</p></div>
-
<div><p>FreeMarker官網:${freeMarkerUrl}</p></div>
-
<div><p>JFreeChart教程:${JFreeChartUrl}</p></div>
-
<!--外部鏈接-->
-
<p>靜態logo圖</p>
-
<div>
-
<img src="${imageUrl}" alt="美團點評" width="512" height="359"/>
-
</div>
-
<!--動態生成的圖片-->
-
<p>氣溫變化對比圖</p>
-
<div>
-
<img src="${picUrl}" alt="我的圖片" width="500" height="270"/>
-
</div>
-
</div>
-
<!--第一頁結束-->
-
<!---分頁標記-->
-
<span style="page-break-after:always;"></span>
-
<!--第二頁開始-->
-
<div class="page">
-
<div>第二頁開始了</div>
-
<div>列表值:</div>
-
<div>
-
<#list scores as item>
-
<div><p>${item}</p></div>
-
</#list>
-
</div>
-
-
</div>
-
<!--第二頁結束-->
-
</body>
-
</html>
2、獲取模板內容並填充數據
-
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("進到這里了,有來無回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("進到這里了,有來無回2");
-
Template template = config.getTemplate(templateFileName);System.out.println( "進到這里了,有來無回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); //根據PDF名稱查找對應的模板名稱
-
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); //根據模板名稱 獲取對應模板
-
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);
-
}
-
}
3、導出模板到PDF文件
-
/**
-
* @description 導出pdf到文件
-
* @param fileName 輸出PDF文件名
-
* @param data 模板所需要的數據
-
*
-
*/
-
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{
-
//設置輸出路徑
-
outputStream= new FileOutputStream(saveFilePath);
-
//設置文檔大小
-
Document document = new Document(PageSize.A4);
-
PdfWriter writer = PdfWriter.getInstance(document, outputStream);
-
-
//設置頁眉頁腳
-
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、測試工具類
-
public String createPDF(Object data, String fileName){
-
//pdf保存路徑
-
try {
-
//設置自定義PDF頁眉頁腳工具類
-
PDFHeaderFooter headerFooter= new PDFHeaderFooter();
-
PDFKit kit= new PDFKit();
-
kit.setHeaderFooterBuilder(headerFooter);
-
//設置輸出路徑
-
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);
-
}
此測試工具類中,要注意幾點:
1)templateBO.setImageUrl("E:/圖片2/004d.jpg");中的參數修改為自己本地有的圖片;
2)程序可能會報找不到模板引擎hello.ftl文件的錯誤,一定要將源碼中的hello.ftl放在本地硬盤對應的目錄中;
六、生成效果圖
七、遇到的坑
1、FreeMarker配置模板文件樣式,在實際PDF生成過程中,可能會出現一些不一致的情形,目前解決方法,就是換種方式調整樣式。
2、字體文件放在resource下,在打包時會報錯,運行mvn -X compile 會看到詳細錯誤:
這是字體文件是二進制的,而maven項目中配置了資源文件的過濾,不能識別二進制文件導致的,plugins中增加下面這個配置就好了:
-
<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文件中,增加分頁標簽: <span style="page-break-after:always;"></span>
八、項目說明
此項目最初是由github上的開源項目經二次開發而成,附github源碼地址:https://github.com/superad/pdf-kit;
但是github上的源碼bug太多,幾乎不能運行,經過一天的測試修改,才完全消除了它的bug;經過測試已經在windows系統,jdk1.7,MyEclipse10中運行成功;此項目只需要在MyEclipse中右擊ReportKit360.java文件,然后選擇run as java application即可,如圖:
下面是整合到web網站中,在網頁中填充內容,然后自動生成pdf文檔后在網頁端查看或者下載。
九、整合到web項目中遇到的坑
1、讀取的模板.ftl文檔時,
發現讀取的內容htmlData開始多了一個?,幾經搜索后發現是因為文檔編碼格式的原因,於是在editplus中將其打開並重新另存為無bom格式的文檔后重新讀取,發現?消失了。
雖然解決了讀取的問題,但是還是沒有解決下載pdf亂碼的問題。
2、又重新debug項目之后發現,不是字體讀取的問題,因為文件夾下的字體是能夠讀取到的,於是懷疑是編碼問題,將所有編碼修改為UTF-8格式,仍沒有解決亂碼問題,又繼續debug項目,幾經細致查看后,感覺應該是文件讀取時是在web容器中的,這一步編碼不太容易修改,於是決定按照讀取是什么編碼就改為什么編碼,最終獲得成功。
web項目代碼結構如下:
啟動服務器后,在瀏覽器中輸入http://localhost:8080/項目名/index.action后回車,即可進入前端輸入pdf文檔內容的頁面,輸入完成后點擊提交,即可下載pdf文檔,生成的文檔格式完全正確,並且沒有亂碼。
web端整合源碼暫時上傳到企鵝群中了:589847567。
參考文章:http://www.jb51.net/article/112366.htm
https://segmentfault.com/a/1190000009160184
無bug版測試源碼下載地址:http://download.csdn.net/detail/pengyufight/9902581