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;}