freemark2pdf
或者是零散的 对某些问题的解决方案
本人再次写一个完整的demo 无bug 可用
我是在spring mvc中应用的
- String basePath = request.getSession().getServletContext()
- .getRealPath("/");
- /* 创建配置 */
- Configuration cfg = new Configuration();
- /* 指定模板存放的路径 */
- cfg.setDirectoryForTemplateLoading(new File(basePath + "/WEB-INF/ftl"));
- cfg.setDefaultEncoding("UTF-8");
- // cfg.setObjectWrapper(new DefaultObjectWrapper());
- /* 从上面指定的模板目录中加载对应的模板文件 */
- // contractTemplate
- Template temp = cfg.getTemplate("contractTemplate.ftl");
- /* 创建数据模型 */
- Map root = new HashMap();
- root.put("user", "Big Joe");
- // Map latest = new HashMap();
- // root.put("latestProduct", latest);
- // latest.put("name", "green mouse");
- /* 将生成的内容写入hello .html中 */
- String file1 = basePath + "html/contractTemplate.html";
- File file = new File(file1);
- if (!file.exists())
- file.createNewFile();
- // Writer out = new FileWriter(file);
- Writer out = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(file), "utf-8"));
- // Writer out = new OutputStreamWriter(System.out);
- temp.process(root, out);
- out.flush();
- String url = new File(file1).toURI().toURL().toString();
- String outputFile = basePath + "html/contractTemplate.pdf";
- OutputStream os = new FileOutputStream(outputFile);
- ITextRenderer renderer = new ITextRenderer();
- // PDFEncryption pdfEncryption = new
- // PDFEncryption(null,null,PdfWriter.ALLOW_PRINTING);
- // renderer.setPDFEncryption(pdfEncryption); //只有打印权限的
- renderer.setDocument(url);
- // 解决中文问题
- ITextFontResolver fontResolver = renderer.getFontResolver();
- try {
- fontResolver.addFont(basePath + "/ui/fonts/simsun.ttc",
- BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
- } catch (DocumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- renderer.layout();
- try {
- renderer.createPDF(os);
- } catch (DocumentException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("转换成功!");
- os.close();
模版中在table 加样式 style="margin-top: 60px;table-layout:fixed; word-break:break-strict;"
这是为了避免 在pdf中显示不全
我的 功能是 通过生成的html生成合同 pdf
最终效果图:

模版文件 就是 ftl文件 你 用html怎么显示就可以生成什么样的pdf
里面可以包含样式 图片
尽量用table布局 这样不会出现 显示到pdf显示不全的 问题
另外 附上freemark、模板文件头部关键 代码主要是处理 中文问题
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html xmlns="http://www.w3.org/1999/xhtml">
- <head>
- <title>要生成的合同文件</title>
- <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
- <style mce_bogus="1" type="text/css">
- body {font-family: SimSun; background:none;margin-left: auto;margin-right: auto;}
- body,html,div,p{ font-size:14px; margin:0px; padding:0px;}
- </style>
- .....
- <div class="table_block">
- <table width="680" border="0" cellspacing="1" cellpadding="1" bgcolor="#CCCCCC" style="table-layout:fixed; word-break:break-strict;">
- ....
另外有关中文换行 问题 有网友问我 后 我发现 解决途径必须需要修改源码 修改后源码包我已经传到 了 附件上core-renderer-R8-0604.jar
有什么疑问可以加我 qq :6637152交流
另外很多网友问我要项目demo 和相关文件
我这个功能是集成在公司项目里的 原先不方便放出来,现在我已经单独抽出一个开源项目供网友参考代码,https://git.oschina.net/zqb/usk.git
网友可以把项目导入eclipse或者myeclipse搜索上面提到的相关代码找到相关功能
- core-renderer.jar (1 MB)
- iText-2.0.8.jar (1.2 MB)
- freemarker2.3.20.jar (994.7 KB)
- iTextAsian.jar (322 KB)
- core-renderer-R8-0604.jar (1.1 MB)
freemark2pdf的更多相关文章
随机推荐
- 小程序红包开发跳坑记 微信小程序红包接口开发过程中遇到的问题 微信小程序红包开发
现在做小程序的越来越多,商家推广也是一个瓶颈,谁不发点红包,都很难找到人来用你的微信小程序了.于是不管你开发什么小程序功能,你或多或少都要用到小程序来发红包吧. 我们自己之前做公众号发红包,做了两三 ...
- webpack简单使用
1 首先npm init 建立package.json文件 npm init 2 然后全局安装webpack npm install webpack -g ...
- 搭建git linux 服务器
假设你已经有sudo权限的用户账号,下面,正式开始安装. 第一步,安装git: for Ubuntu或Debian $ sudo apt-get install git for Centos 更新一下 ...
- python斐波那契数列复杂度
契数列 概述: 斐波那契数列,又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.34.……在数学上,斐波纳契数列以如下被以递归的方法定义:F(0)=0,F(1)=1,F(n ...
- 创建外网 ext
虽然外部网络是已经存在的网络,但我们还是需要在 Neutron 中定义外部网络的对象,这样 router 才知道如何将租户网络和外部网络连接起来. 上一节我们已经为创建外部网络配置了ML2,本节将通过 ...
- ZOJ1608 Two Circles and a Rectangle
Time Limit: 2 Seconds Memory Limit: 65536 KB Give you two circles and a rectangle, your task is ...
- DataBinder.Eval值的判断
原文发布时间为:2009-04-10 -- 来源于本人的百度文章 [由搬家工具导入] 问:如何对<%# DataBinder.Eval(Container.DataItem,"Ly_R ...
- 强连通分量(Tarjan)模板
贴模板,备忘. 模板1: #include<iostream> #include<cstring> #include<cmath> #include<cstd ...
- 洛谷 P1784 数独[DFS/回溯]
To 洛谷.1784 数独类似题:CODEVS.4966 简单数独(4*4数独) CODEVS.2924 数独挑战) 题目描述 数独是根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行 ...
- TopCoder SRM 682 Div1 Problem 450 SuccessfulMerger (环套树 + 分类讨论)
题意 给定一个$n$个点$n$条边的无向图,现在要把这个图进行若干次操作,并选择一个点作为首都. 要求除首都外的任意两个点$u$, $v$,从$u$走到$v$必须经过这个首都. 操作为合并两个相邻的 ...