zoukankan      html  css  js  c++  java
  • freemark2pdf

    网上已经有比较多的例子 写这个 但是很多都是简单的 demo,而且有很多隐藏的问题
    或者是零散的 对某些问题的解决方案

    本人再次写一个完整的demo  无bug 可用

    我是在spring mvc中应用的

    Java代码  收藏代码
    1. String basePath = request.getSession().getServletContext()  
    2.                 .getRealPath("/");  
    3.         /* 创建配置 */  
    4.         Configuration cfg = new Configuration();  
    5.         /* 指定模板存放的路径 */  
    6.         cfg.setDirectoryForTemplateLoading(new File(basePath + "/WEB-INF/ftl"));  
    7.         cfg.setDefaultEncoding("UTF-8");  
    8.         // cfg.setObjectWrapper(new DefaultObjectWrapper());  
    9.   
    10.         /* 从上面指定的模板目录中加载对应的模板文件 */  
    11.         // contractTemplate  
    12.         Template temp = cfg.getTemplate("contractTemplate.ftl");  
    13.   
    14.         /* 创建数据模型 */  
    15.         Map root = new HashMap();  
    16.         root.put("user", "Big Joe");  
    17.         // Map latest = new HashMap();  
    18.         // root.put("latestProduct", latest);  
    19.         // latest.put("name", "green mouse");  
    20.   
    21.         /* 将生成的内容写入hello .html中 */  
    22.   
    23.         String file1 = basePath + "html/contractTemplate.html";  
    24.         File file = new File(file1);  
    25.         if (!file.exists())  
    26.             file.createNewFile();  
    27.         // Writer out = new FileWriter(file);  
    28.         Writer out = new BufferedWriter(new OutputStreamWriter(  
    29.                 new FileOutputStream(file), "utf-8"));  
    30.         // Writer out = new OutputStreamWriter(System.out);  
    31.         temp.process(root, out);  
    32.         out.flush();  
    33.   
    34.         String url = new File(file1).toURI().toURL().toString();  
    35.         String outputFile = basePath + "html/contractTemplate.pdf";  
    36.         OutputStream os = new FileOutputStream(outputFile);  
    37.   
    38.         ITextRenderer renderer = new ITextRenderer();  
    39.         // PDFEncryption pdfEncryption = new  
    40.         // PDFEncryption(null,null,PdfWriter.ALLOW_PRINTING);  
    41.         // renderer.setPDFEncryption(pdfEncryption); //只有打印权限的  
    42.         renderer.setDocument(url);  
    43.   
    44.         // 解决中文问题  
    45.         ITextFontResolver fontResolver = renderer.getFontResolver();  
    46.         try {  
    47.             fontResolver.addFont(basePath + "/ui/fonts/simsun.ttc",  
    48.                     BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);  
    49.         } catch (DocumentException e) {  
    50.             // TODO Auto-generated catch block  
    51.             e.printStackTrace();  
    52.         }  
    53.   
    54.         renderer.layout();  
    55.         try {  
    56.             renderer.createPDF(os);  
    57.         } catch (DocumentException e) {  
    58.             // TODO Auto-generated catch block  
    59.             e.printStackTrace();  
    60.         }  
    61.         System.out.println("转换成功!");  
    62.         os.close();  



    模版中在table 加样式 style="margin-top: 60px;table-layout:fixed; word-break:break-strict;"

    这是为了避免 在pdf中显示不全 
    我的 功能是 通过生成的html生成合同 pdf

    最终效果图:


    模版文件 就是 ftl文件 你 用html怎么显示就可以生成什么样的pdf

    里面可以包含样式  图片


    尽量用table布局 这样不会出现 显示到pdf显示不全的 问题


    另外 附上freemark、模板文件头部关键 代码主要是处理 中文问题
    Java代码  收藏代码
    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">    
    2. <html xmlns="http://www.w3.org/1999/xhtml">    
    3.   <head>  
    4.     <title>要生成的合同文件</title>  
    5.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  
    6. <style mce_bogus="1" type="text/css">  
    7. body {font-family: SimSun; background:none;margin-left: auto;margin-right: auto;}  
    8. body,html,div,p{ font-size:14px; margin:0px; padding:0px;}  
    9.   
    10.   
    11. </style>  
    12. .....  
    13. <div class="table_block">  
    14. <table width="680" border="0" cellspacing="1" cellpadding="1" bgcolor="#CCCCCC"  style="table-layout:fixed; word-break:break-strict;">  
    15. ....  



    另外有关中文换行 问题 有网友问我 后 我发现 解决途径必须需要修改源码 修改后源码包我已经传到 了 附件上core-renderer-R8-0604.jar
    有什么疑问可以加我 qq :6637152交流

    另外很多网友问我要项目demo 和相关文件
    我这个功能是集成在公司项目里的 原先不方便放出来,现在我已经单独抽出一个开源项目供网友参考代码,https://git.oschina.net/zqb/usk.git

    网友可以把项目导入eclipse或者myeclipse搜索上面提到的相关代码找到相关功能
  • 相关阅读:
    Stars in Your Window POJ
    Adding New Machine ZOJ
    洛谷 P3400 仓鼠窝
    django启动时报错:Apps aren't loaded yet.
    netstat命令简单使用
    iostat命令简单使用
    linux下查找指定时间内修改过的或新建的文件
    nginx学习之压缩解压篇(七)
    nginx学习之反向代理篇(六)
    nginx学习之静态内容篇(五)
  • 原文地址:https://www.cnblogs.com/littlemonk/p/5679508.html
Copyright © 2011-2022 走看看