欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

iText将html转化为pdf

程序员文章站 2024-03-19 11:59:22
...
1 将如下jar包导入工程:
core-renderer.jar
core-renderer-minimal.jar
iText-2.0.8.jar
iTextAsian.jar
xml-apis-xerces-2.9.1.jar

2 Code:
[/code]
//将html转化后并进行下载
public String reportPdf(MessageResources resources,
Locale locale, HttpSession session, HttpServletRequest request,
HttpServletResponse response) throws Exception {
response.reset();
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment;filename="+java.net.URLEncoder.encode("itextConvertHtml","UTF-8")+".pdf");
OutputStream os = response.getOutputStream();
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
//用于window
fontResolver.addFont("C:/Windows/fonts/simsun.ttc",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //设置中文字体
//Linux系统需引用simsun.ttc(此文件在系统中创建),window系统直接引用c:/windows/font/simsun.ttc
//fontResolver.addFont(this.getServlet().getServletContext().getRealPath("/")+"/images/simsun.ttc",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
StringBuffer html = new StringBuffer();
// DOCTYPE 必需写否则类似于 这样的字符解析会出现错误
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
html.append("<head>");
html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
html.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>");
html.append("</head>").append("<body>");
html.append("<img src='itextConvertHtml1.jpg'/><img src='itextConvertHtml2.jpg'/>");
html.append("</body></html>");
try{
renderer.setDocumentFromString(html.toString());
// 解决图片的相对路径问题(在系统的images/upload去寻找itextConvertHtml1.jpg和itextConvertHtml1.jpg)
renderer.getSharedContext().setBaseURL("file:/"+this.getServlet().getServletContext().getRealPath("/")+"images/upload/");
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
}catch(Exception ex){}
return null;
}


//将html转化后生成到本地磁盘
public String outPutPdf(MessageResources resources,
Locale locale, HttpSession session, HttpServletRequest request,
HttpServletResponse response) throws Exception {
String outputFile = "E:/itextConvertHtml.pdf";
OutputStream os = new FileOutputStream(outputFile);
ITextRenderer renderer = new ITextRenderer();
ITextFontResolver fontResolver = renderer.getFontResolver();
fontResolver.addFont("C:/Windows/fonts/simsun.ttc",
BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED); //设置中文字体
StringBuffer html = new StringBuffer();
// DOCTYPE 必需写否则类似于 这样的字符解析会出现错误
html.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
html.append("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
html.append("<head>");
html.append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />");
html.append("<style type=\"text/css\" mce_bogus=\"1\">body {font-family: SimSun;}</style>");
html.append("</head>").append("<body>");
html.append("<img src='itextConvertHtml1.jpg'/><img src='itextConvertHtml2.jpg'/>");
html.append("</body></html>");
try{
renderer.setDocumentFromString(html.toString());
// 解决图片的相对路径问题(在系统的images/upload去寻找itextConvertHtml1.jpg和itextConvertHtml1.jpg)
renderer.getSharedContext().setBaseURL("file:/"+this.getServlet().getServletContext().getRealPath("/")+"images/upload/");
renderer.layout();
renderer.createPDF(os);
os.flush();
os.close();
}catch(Exception ex){}
return null;
}

3 在使用itext将html转化为pdf时,html的标签必须是开始-结束,另外itext中有部分不支持html的标签。
相关标签: itext

上一篇: Java 自带 MD5 加密

下一篇: