java根據模板導出PDF詳細教程


原文: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、模板配置

 

  1.  
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  2.  
    <html xmlns="http://www.w3.org/1999/xhtml">
  3.  
    <head>
  4.  
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
  5.  
    <meta http-equiv="Content-Style-Type" content="text/css"/>
  6.  
    <title></title>
  7.  
    <style type="text/css">
  8.  
    body {
  9.  
    font-family: pingfang sc light;
  10.  
    }
  11.  
    .center{
  12.  
    text-align: center;
  13.  
    width: 100%;
  14.  
    }
  15.  
    </style>
  16.  
    </head>
  17.  
    <body>
  18.  
    <!--第一頁開始-->
  19.  
    <div class="page" >
  20.  
    <div class="center"><p>${templateName}</p></div>
  21.  
    <div><p>iText官網:${ITEXTUrl}</p></div>
  22.  
    <div><p>FreeMarker官網:${freeMarkerUrl}</p></div>
  23.  
    <div><p>JFreeChart教程:${JFreeChartUrl}</p></div>
  24.  
    <!--外部鏈接-->
  25.  
    <p>靜態logo圖</p>
  26.  
    <div>
  27.  
    <img src="${imageUrl}" alt="美團點評" width="512" height="359"/>
  28.  
    </div>
  29.  
    <!--動態生成的圖片-->
  30.  
    <p>氣溫變化對比圖</p>
  31.  
    <div>
  32.  
    <img src="${picUrl}" alt="我的圖片" width="500" height="270"/>
  33.  
    </div>
  34.  
    </div>
  35.  
    <!--第一頁結束-->
  36.  
    <!---分頁標記-->
  37.  
    <span style="page-break-after:always;"></span>
  38.  
    <!--第二頁開始-->
  39.  
    <div class="page">
  40.  
    <div>第二頁開始了</div>
  41.  
    <div>列表值:</div>
  42.  
    <div>
  43.  
    <#list scores as item>
  44.  
    <div><p>${item}</p></div>
  45.  
    </#list>
  46.  
    </div>
  47.  
     
  48.  
    </div>
  49.  
    <!--第二頁結束-->
  50.  
    </body>
  51.  
    </html>



 

2、獲取模板內容並填充數據

 

  1.  
    public static String getContent(String fileName,Object data){
  2.  
     
  3.  
    String templatePath=getPDFTemplatePath(fileName).replace( "\\", "/");
  4.  
    String templateFileName=getTemplateName(templatePath).replace( "\\", "/");
  5.  
    String templateFilePath=getTemplatePath(templatePath).replace( "\\", "/");
  6.  
    System.out.println( "templatePath:"+templatePath);
  7.  
    System.out.println( "templateFileName:"+templateFileName);
  8.  
    System.out.println( "templateFilePath:"+templateFilePath);
  9.  
    if(StringUtils.isEmpty(templatePath)){
  10.  
    throw new FreeMarkerException("templatePath can not be empty!");
  11.  
    }
  12.  
    try{System.out.println("進到這里了,有來無回1");
  13.  
    Configuration config = new Configuration(Configuration.VERSION_2_3_25);
  14.  
    config.setDefaultEncoding( "UTF-8");
  15.  
    config.setDirectoryForTemplateLoading( new File(templateFilePath));
  16.  
    config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
  17.  
    config.setLogTemplateExceptions( false);System.out.println("進到這里了,有來無回2");
  18.  
    Template template = config.getTemplate(templateFileName);System.out.println( "進到這里了,有來無回3");
  19.  
    StringWriter writer = new StringWriter();
  20.  
    template.process(data, writer);
  21.  
    writer.flush();
  22.  
    String html = writer.toString();
  23.  
    return html;
  24.  
    } catch (Exception ex){
  25.  
    throw new FreeMarkerException("FreeMarkerUtil process fail",ex);
  26.  
    }
  27.  
    }
  1.  
    public static String getContent(String fileName,Object data){
  2.  
     
  3.  
    String templatePath=getPDFTemplatePath(fileName); //根據PDF名稱查找對應的模板名稱
  4.  
    String templateFileName=getTemplateName(templatePath);
  5.  
    String templateFilePath=getTemplatePath(templatePath);
  6.  
    if(StringUtils.isEmpty(templatePath)){
  7.  
    throw new FreeMarkerException("templatePath can not be empty!");
  8.  
    }
  9.  
    try{
  10.  
    Configuration config = new Configuration(Configuration.VERSION_2_3_25);//FreeMarker配置
  11.  
    config.setDefaultEncoding( "UTF-8");
  12.  
    config.setDirectoryForTemplateLoading( new File(templateFilePath));//注意這里是模板所在文件夾,不是文件
  13.  
    config.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
  14.  
    config.setLogTemplateExceptions( false);
  15.  
    Template template = config.getTemplate(templateFileName); //根據模板名稱 獲取對應模板
  16.  
    StringWriter writer = new StringWriter();
  17.  
    template.process(data, writer); //模板和數據的匹配
  18.  
    writer.flush();
  19.  
    String html = writer.toString();
  20.  
    return html;
  21.  
    } catch (Exception ex){
  22.  
    throw new FreeMarkerException("FreeMarkerUtil process fail",ex);
  23.  
    }
  24.  
    }

 

 

3、導出模板到PDF文件

  1.  
    /**
  2.  
    * @description 導出pdf到文件
  3.  
    * @param fileName 輸出PDF文件名
  4.  
    * @param data 模板所需要的數據
  5.  
    *
  6.  
    */
  7.  
    public String exportToFile(String fileName,Object data){
  8.  
    try {
  9.  
    String htmlData= FreeMarkerUtil.getContent(fileName, data);
  10.  
    if(StringUtils.isEmpty(saveFilePath)){
  11.  
    saveFilePath=getDefaultSavePath(fileName);
  12.  
    }
  13.  
    File file= new File(saveFilePath);
  14.  
    if(!file.getParentFile().exists()){
  15.  
    file.getParentFile().mkdirs();
  16.  
    }
  17.  
    FileOutputStream outputStream= null;
  18.  
    try{
  19.  
    //設置輸出路徑
  20.  
    outputStream= new FileOutputStream(saveFilePath);
  21.  
    //設置文檔大小
  22.  
    Document document = new Document(PageSize.A4);
  23.  
    PdfWriter writer = PdfWriter.getInstance(document, outputStream);
  24.  
     
  25.  
    //設置頁眉頁腳
  26.  
    PDFBuilder builder = new PDFBuilder(headerFooterBuilder,data);
  27.  
    builder.setPresentFontSize( 10);
  28.  
    writer.setPageEvent(builder);
  29.  
     
  30.  
    //輸出為PDF文件
  31.  
    convertToPDF(writer,document,htmlData);
  32.  
    } catch(Exception ex){
  33.  
    throw new PDFException("PDF export to File fail",ex);
  34.  
    } finally{
  35.  
    IOUtils.closeQuietly(outputStream);
  36.  
    }
  37.  
     
  38.  
    } catch (Exception e) {
  39.  
    e.printStackTrace();
  40.  
    }
  41.  
    return saveFilePath;
  42.  
    }

 

4、測試工具類

  1.  
    public String createPDF(Object data, String fileName){
  2.  
    //pdf保存路徑
  3.  
    try {
  4.  
    //設置自定義PDF頁眉頁腳工具類
  5.  
    PDFHeaderFooter headerFooter= new PDFHeaderFooter();
  6.  
    PDFKit kit= new PDFKit();
  7.  
    kit.setHeaderFooterBuilder(headerFooter);
  8.  
    //設置輸出路徑
  9.  
    kit.setSaveFilePath( "D:/Users/hello.pdf");
  10.  
    String saveFilePath=kit.exportToFile(fileName,data);
  11.  
    return saveFilePath;
  12.  
    } catch (Exception e) {
  13.  
    System.out.println( "竟然失敗了,艹!");
  14.  
    e.printStackTrace();
  15.  
    // log.error("PDF生成失敗{}", ExceptionUtils.getFullStackTrace(e));
  16.  
    log.error( "PDF生成失敗{}");
  17.  
    return null;
  18.  
    }
  19.  
    }
  20.  
     
  21.  
    public static void main(String[] args) {
  22.  
    ReportKit360 kit= new ReportKit360();
  23.  
    TemplateBO templateBO= new TemplateBO();
  24.  
    templateBO.setTemplateName( "Hello iText! Hello freemarker! Hello jFreeChart!");
  25.  
    templateBO.setFreeMarkerUrl( "http://www.zheng-hang.com/chm/freemarker2_3_24/ref_directive_if.html");
  26.  
    templateBO.setITEXTUrl( "http://developers.itextpdf.com/examples-itext5");
  27.  
    templateBO.setJFreeChartUrl( "http://www.yiibai.com/jfreechart/jfreechart_referenced_apis.html");
  28.  
    templateBO.setImageUrl( "E:/圖片2/004d.jpg");
  29.  
    List<String> scores= new ArrayList<String>();
  30.  
    scores.add( "94");
  31.  
    scores.add( "95");
  32.  
    scores.add( "98");
  33.  
    templateBO.setScores(scores);
  34.  
    List<Line> lineList=getTemperatureLineList();
  35.  
    DefaultLineChart lineChart= new DefaultLineChart();
  36.  
    lineChart.setHeight( 500);
  37.  
    lineChart.setWidth( 300);
  38.  
    String picUrl=lineChart.draw(lineList, 0);
  39.  
    templateBO.setPicUrl(picUrl);System.out.println( "picUrl:"+picUrl);
  40.  
    String path= kit.createPDF(templateBO, "hello.pdf");
  41.  
    System.out.println( "打印:"+path);
  42.  
    }


此測試工具類中,要注意幾點:

        1)templateBO.setImageUrl("E:/圖片2/004d.jpg");中的參數修改為自己本地有的圖片;

         2)程序可能會報找不到模板引擎hello.ftl文件的錯誤,一定要將源碼中的hello.ftl放在本地硬盤對應的目錄中;

 

六、生成效果圖

 

 

七、遇到的坑

1、FreeMarker配置模板文件樣式,在實際PDF生成過程中,可能會出現一些不一致的情形,目前解決方法,就是換種方式調整樣式。
2、字體文件放在resource下,在打包時會報錯,運行mvn -X compile 會看到詳細錯誤:
這是字體文件是二進制的,而maven項目中配置了資源文件的過濾,不能識別二進制文件導致的,plugins中增加下面這個配置就好了:

 

  1.  
    <build>
  2.  
    <resources>
  3.  
    <resource>
  4.  
    <directory>src/main/resources</directory>
  5.  
    <filtering>true</filtering>
  6.  
    </resource>
  7.  
    </resources>
  8.  
    <!--增加的配置,過濾ttf文件的匹配-->
  9.  
    <plugins>
  10.  
    <plugin>
  11.  
    <groupId>org.apache.maven.plugins</groupId>
  12.  
    <artifactId>maven-resources-plugin</artifactId>
  13.  
    <version>2.7</version>
  14.  
    <configuration>
  15.  
    <encoding>UTF-8</encoding>
  16.  
    <nonFilteredFileExtensions>
  17.  
    <nonFilteredFileExtension>ttf</nonFilteredFileExtension>
  18.  
    </nonFilteredFileExtensions>
  19.  
    </configuration>
  20.  
    </plugin>
  21.  
    </plugins>
  22.  
    </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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM