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

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

我是在spring mvc中应用的

  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. // contractTemplate
  11. Template temp = cfg.getTemplate("contractTemplate.ftl");
  12. /* 创建数据模型 */
  13. Map root = new HashMap();
  14. root.put("user", "Big Joe");
  15. // Map latest = new HashMap();
  16. // root.put("latestProduct", latest);
  17. // latest.put("name", "green mouse");
  18. /* 将生成的内容写入hello .html中 */
  19. String file1 = basePath + "html/contractTemplate.html";
  20. File file = new File(file1);
  21. if (!file.exists())
  22. file.createNewFile();
  23. // Writer out = new FileWriter(file);
  24. Writer out = new BufferedWriter(new OutputStreamWriter(
  25. new FileOutputStream(file), "utf-8"));
  26. // Writer out = new OutputStreamWriter(System.out);
  27. temp.process(root, out);
  28. out.flush();
  29. String url = new File(file1).toURI().toURL().toString();
  30. String outputFile = basePath + "html/contractTemplate.pdf";
  31. OutputStream os = new FileOutputStream(outputFile);
  32. ITextRenderer renderer = new ITextRenderer();
  33. // PDFEncryption pdfEncryption = new
  34. // PDFEncryption(null,null,PdfWriter.ALLOW_PRINTING);
  35. // renderer.setPDFEncryption(pdfEncryption); //只有打印权限的
  36. renderer.setDocument(url);
  37. // 解决中文问题
  38. ITextFontResolver fontResolver = renderer.getFontResolver();
  39. try {
  40. fontResolver.addFont(basePath + "/ui/fonts/simsun.ttc",
  41. BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
  42. } catch (DocumentException e) {
  43. // TODO Auto-generated catch block
  44. e.printStackTrace();
  45. }
  46. renderer.layout();
  47. try {
  48. renderer.createPDF(os);
  49. } catch (DocumentException e) {
  50. // TODO Auto-generated catch block
  51. e.printStackTrace();
  52. }
  53. System.out.println("转换成功!");
  54. os.close();

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

这是为了避免 在pdf中显示不全 

我的 功能是 通过生成的html生成合同 pdf

最终效果图:

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

里面可以包含样式  图片

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

另外 附上freemark、模板文件头部关键 代码主要是处理 中文问题

  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. </style>
  10. .....
  11. <div class="table_block">
  12. <table width="680" border="0" cellspacing="1" cellpadding="1" bgcolor="#CCCCCC"  style="table-layout:fixed; word-break:break-strict;">
  13. ....

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

有什么疑问可以加我 qq :6637152交流

另外很多网友问我要项目demo 和相关文件

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

网友可以把项目导入eclipse或者myeclipse搜索上面提到的相关代码找到相关功能

freemark2pdf的更多相关文章

随机推荐

  1. ios - 工具类

    这几天看项目,把俺旁边小哥哥的一个工具类相中了,希望对大家有所帮助哦~~~~~~~~~ // // PLZ_Tool.h // // Created by penglaizhi on 2017/7/3 ...

  2. pat 团体天梯赛 L2-007. 家庭房产

    L2-007. 家庭房产 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序给定每个人的家庭成员和其自己名下的房产,请你统计出每个家庭的人口数.人均房产面积及房产 ...

  3. Scala 封装可break和continue的foreach循环

    发现scala里没有break和continue, 有时候实在是需要的话就要自己try catch异常,代码看起来有点蛋疼, 所以封装了一个可break和continue的foreach. impor ...

  4. Guice 4.1教程

    Guice是Google开发的一个开源轻量级的依赖注入框架,运行速度快,使用简单. 项目地址:https://github.com/google/guice/ 最新的版本是4.1,本文基于此版本. 0 ...

  5. Cryptography I 学习笔记 --- 基于陷门置换的公钥加密

    RSA算法的工作流程 1. 生成公钥私钥 生成两个素数p和q,计算n=p*q,计算φ(n)=n-p-q+1,然后生成e与d,使 e * d = 1 mod φ(n). 然后以(n, e)作为公钥,(n ...

  6. angular6安装

    中文 https://www.angular.cn/ 二.下载 1.安装 node.js https://nodejs.org/en/ 2.删除老angular-cli npm uninstall - ...

  7. MySQL的INFORMATION_SCHEMA数据库简介

    大家在安装或使用MYSQL时,会发现除了自己安装的数据库以外,还有一个 information_schema数据库.information_schema数据库是做什么用的呢,使用WordPress博客 ...

  8. IT人为了自己父母和家庭,更得注意自己的身体和心理健康

    我前一阵在一家互联网公司,工作节奏是995,忙的时候,要晚上10点才能离开公司,有时候周六还得加班.自己感觉身体状况有所下降,且听说其它一个组,在体检后多少都查出问题来,细思极恐. 有时候确实忙,那么 ...

  9. MapReduce1 工作机制

    本文转自:Hadoop MapReduce 工作机制 工作流程 作业配置 作业提交 作业初始化 作业分配 作业执行 进度和状态更新 作业完成 错误处理 作业调度 shule(mapreduce核心)和 ...

  10. 洛谷——P1130 红牌

    题目描述 某地临时居民想获得长期居住权就必须申请拿到红牌.获得红牌的过程是相当复杂 ,一共包括N个步骤.每一步骤都由政府的某个工作人员负责检查你所提交的材料是否符合条件.为了加快进程,每一步政府都派了 ...