首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > PowerDesigner >

iText将html转化作pdf

2013-11-03 
iText将html转化为pdf1 将如下jar包导入工程:core-renderer.jarcore-renderer-minimal.jariText-2.0.8.jar

iText将html转化为pdf
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:

//将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();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;}

//将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的标签。

热点排行