freemarker+ITextRenderer 生成html转pdf
网上已经有比较多的例子 写这个 但是很多都是简单的 demo,而且有很多隐藏的问题 或者是零散的 对某些问题的解决方案
本人再次写一个完整的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;">
....
另外很多网友问我要项目demo 和相关文件 我这个功能是集成在公司项目里的 原先不方便放出来,现在我已经单独抽出一个开源项目供网友参考代码,https://git.oschina.net/zqb/usk.git
网友可以把项目导入eclipse或者myeclipse搜索上面提到的相关代码找到相关功能
用过之后中文可以换行,但是现在编程了英文和数字不换行了 脑壳痛
Error:(275, 29) java: 无法访问com.lowagie.text.DocumentException 找不到com.lowagie.text.DocumentException的类文件 这样引入 org.xhtmlrenderer core-renderer R8 system ${basedir}/src/main/resources/lib/core-renderer-R8-0604-.jar 和itext,总是会报上面的错误,这是什么原因
楼主你好,我想问下,如何在maven中使用你的jar包呢
楼主,您知道 @page WordSection1 {size:595.3pt 841.9pt; margin:36.0pt 36.0pt 36.0pt 36.0pt; layout-grid:15.6pt;} div.WordSection1 {page:WordSection1;}的定义了这么个page后,生成的pdf,第一页是空白的,为什么吗?我把这段去掉,第一页就不是空白了。
楼主 我想问下我是那个中文不换行的问题,你上面那个jar包 我下载下来是乱码 是怎么回事呢
大神在不,我这边生成完html转成PDF时中文直接没有了: 不知道啥问题,请楼主帮忙解答下 : HTML:
代码: try { /* 创建配 置 / Configuration cfg = new Configuration(); / 指定模板存放的路径 */
做了好多个模板后客户要求加页码,这种方式导出pdf网上没找到解决方案啊 ,大神可否解决
那生成的html文件里面有js的话js会执行吗?我生成的html文件的js代码在转pdf的时候都在报错
您好,怎么设置PSF为A4横向
???
这哪里慢,楼主说下谢谢
freemarker有专门的 读取后台值的标签 你不要用其他jstl的标签
想问一下 问什么freemaker不识别标签呢 有数据标签的地方总会报 元素内容必须由格式正确的字符数据或标记组成。 普通标签会直接显示 &{XXXX} 我这是哪里出问题了 请大神指教一下
还有一个freemark的jar包你网上搜下很多的
楼主,这里都使用到哪些jar包,可否贴出来,谢谢!
:lol: 合同已经搞定 ... 其中还遇到一个头疼的问题 ,就是 一行过长的话 ,不会自动换行~ 不过解决了!
我抽空 整理写一个 到时候会放到博客里
:cry:
合同又来了... 能提供个demo么... 光是代码 还有很多封装阿 .
页面上是否中了中文字体 设置 是否后台做了中文字体设置
你好,为什么我生成的pdf都不显示中文呢? 就是这样,中文好像都被抛弃了:
(1) (2) (3) Free sale18:00
(4) Free saleFree sale