zoukankan      html  css  js  c++  java
  • 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.  
      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

  • 相关阅读:
    @@@并发实战
    @@@jvm实战
    @@@spring Boot环境下dubbo+zookeeper实战
    FastJson 支持配置的PropertyNamingStrategy四种策略
    利用MySQL统计一列中不同值的数量方法示例
    Springboot+websocket+定时器实现消息推送
    Spring AOP中args()、arg-names、argNames
    squid,nginx,lighttpd反向代理的区别
    HashMap底层实现原理/HashMap与HashTable区别/HashMap与HashSet区别
    HTTP 错误 500.24
  • 原文地址:https://www.cnblogs.com/shihaiming/p/9591685.html
Copyright © 2011-2022 走看看