您的位置:首页 > Web前端 > HTML

html生成pdf

2015-08-23 00:00 525 查看
摘要: itext、volecty、pdf、html

1、 准备jar
commons-collections-3.2.1.jar
commons-lang-2.6.jar
core-renderer-chinese-1.0.0.jar
iText-2.0.8.jar
velocity-1.6.4.jar
2、 创建模板result.html
<!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>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<style type="text/css">
*{font-family: "STFangsong";}
</style>
</head>
<body>
文本内容
</body>
</html>
3、 准备字体D:\ STFANGSO.TTF
4、 调用工具类
5、 import java.io.BufferedOutputStream;
6、 import java.io.File;
7、 import java.io.FileOutputStream;
8、 import java.io.OutputStream;
9、 import java.io.StringWriter;
10、 import java.util.Map;
11、 import java.util.Properties;
12、 import org.apache.velocity.VelocityContext;
13、 import org.apache.velocity.app.Velocity;
14、 import org.apache.velocity.app.VelocityEngine;
15、 import org.xhtmlrenderer.pdf.ITextFontResolver;
16、 import org.xhtmlrenderer.pdf.ITextRenderer;
17、
18、 import com.lowagie.text.pdf.BaseFont;
27、 public class PdfUtils{
28、
29、 private static final String PDF = ".pdf";
30、 private static final String HTML = ".html";
31、
32、 private static final VelocityEngine ENGINE = new VelocityEngine();
33、
34、 static {
35、 Properties properties = new Properties();
36、 properties.put(Velocity.FILE_RESOURCE_LOADER_PATH, "D:\\");
37、 try {
38、 ENGINE.init(properties);
39、 } catch (Exception e) {
40、 // TODO Auto-generated catch block
41、 e.printStackTrace();
42、 }
43、 }
44、
45、 /**
46、 * 根据模板在指定目录中生成文件,模板放在类路径的"template/pdf/"下
47、 *
48、 * @param fileName
49、 * 要生成文件名,不能为空,可不带后缀。
50、 * @param templatePath
51、 * 模板文件,不能为空,放在类路径下的"template/pdf/"中,可不带后缀。
52、 * @param model
53、 * 业务数据模型,可为空。
54、 * @return 生成的文件全路径
55、 *
56、 * 如果创建失败 ,则抛出业务异常
57、 */
58、 public static String create(String fileName, String templatePath,
59、 Map<String, Object> model) {
60、 // 补齐后缀".html"
61、 if (!templatePath.endsWith(HTML)) {
62、 templatePath = templatePath + HTML;
63、 }
64、 // 补齐后缀".pdf"
65、 if (!fileName.endsWith(PDF)) {
66、 fileName = fileName + PDF;
67、 }
68、 // 构建保存文件夹
69、 String savePath = "D:\\";
70、 // 如果保存文件夹不存在,则创建
71、 File saveFile = new File(savePath);
72、 if (!saveFile.exists()) {
73、 saveFile.mkdirs();
74、 }
75、 // 构建完整保存路径
76、 savePath = savePath + File.separator + fileName;
77、 // 如果文件已存在,则删除
78、 saveFile = new File(savePath);
79、 if (saveFile.exists()) {
80、 boolean flag = saveFile.delete();
81、 }
82、 OutputStream os = null;
83、 try {
84、 StringWriter sw = new StringWriter();
85、 ENGINE.getTemplate(templatePath, "UTF-8").merge(
86、 new VelocityContext(model), sw);
87、 String html = sw.toString();
88、 ITextRenderer renderer = new ITextRenderer();
89、 renderer.setDocumentFromString(html);
90、 // 解决中文汉字显示问题
92、 ITextFontResolver fontResolver = renderer.getFontResolver();
93、 String baseURL = getBaseURL();

96、
fontResolver.addFont(baseURL +
"STFANGSO.TTF"
,

97、 BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
98、 // 解决图片加载问题
99、 renderer.getSharedContext().setBaseURL(baseURL);
100、 renderer.layout();
101、 os = new BufferedOutputStream(new FileOutputStream(savePath));
102、 renderer.createPDF(os);
103、 os.flush();
104、 } catch (Exception e) {
105、 } finally {
106、 try {
107、 if (os != null) {
108、 os.close();
109、 }
110、 } catch (Exception e) {
111、 }
112、 }
113、 System.out.println(savePath);
114、 return savePath;
115、 }
116、
117、 private static String getBaseURL() {
118、 // 解决pdf图片问题,以绝对路径加载
119、 // 注意,直接部署war会导致运行时获取不到,必须改为部署解压后的文件夹。
120、 String warPath = "D:\\";
121、
122、 return "D:\\";
123、 }
124、
125、 }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  itext volecty pdf html