`
wuzijingaip
  • 浏览: 319142 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java HTML直接导出PDF

 
阅读更多

Java  HTML直接导出PDF

对于java中如何从html中直接导出pdf,有很多的开源代码,这里个人用itext转。

首先需要的包有:core-renderer-1.0.jar

core-renderer-R8pre1.jar

core-renderer.jar

iText-2.0.8.jar

jtidy-4aug2000r7-dev.jar

Tidy.jar

iTextAsian.jar

java代码的话就比较简单了。具体是先用Tidy将html转换为xhtml,将xhtml转换为其它各种格式的。虽然在转化到pdf时也是用的iText。代码如下:

Java代码  收藏代码
  1. //struts1.x中  
Java代码  收藏代码
  1. else if("Html2Pdf".equalsIgnoreCase(action)){  
  2.     exportPdfFile("http://localhost:8080/jsp/test.jsp");  
  3.     return null;  
  4. }  
  5.   
  6. // 导出pdf add by huangt 2012.6.1  
  7.     public File exportPdfFile(String urlStr) throws BaseException {  
  8.         // String outputFile = this.fileRoot + "/" +  
  9.         // ServiceConstants.DIR_PUBINFO_EXPORT + "/" + getFileName() + ".pdf";  
  10.         String outputFile = "d:/test3.pdf";  
  11.         OutputStream os;  
  12.         try {  
  13.             os = new FileOutputStream(outputFile);  
  14.   
  15.             ITextRenderer renderer = new ITextRenderer();  
  16.   
  17.             String str = getHtmlFile(urlStr);  
  18.             renderer.setDocumentFromString(str);  
  19.             ITextFontResolver fontResolver = renderer.getFontResolver();  
  20.               
  21.             fontResolver.addFont("C:/WINDOWS/Fonts/SimSun.ttc",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);// 宋体字  
  22.             fontResolver.addFont("C:/WINDOWS/Fonts/Arial.ttf",BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);// 宋体字  
  23.             renderer.layout();  
  24.   
  25.             renderer.createPDF(os);  
  26.   
  27.             System.out.println("转换成功!");  
  28.             os.flush();  
  29.             os.close();  
  30.             return new File(outputFile);  
  31.         } catch (FileNotFoundException e) {  
  32.             // logger.error("不存在文件!" + e.getMessage());  
  33.             throw new BaseException(e);  
  34.         } catch (DocumentException e) {  
  35.             // logger.error("生成pdf时出错了!" + e.getMessage());  
  36.             throw new BaseException(e);  
  37.         } catch (IOException e) {  
  38.             // logger.error("pdf出错了!" + e.getMessage());  
  39.             throw new BaseException(e);  
  40.         }  
  41.   
  42.     }  
  43.   
  44.     // 读取页面内容 add by huangt 2012.6.1  
  45.     public String getHtmlFile(String urlStr) throws BaseException {  
  46.         URL url;  
  47.         try {  
  48.             if (urlStr.indexOf("?") != -1) {  
  49.                 urlStr = urlStr + "&locale="  
  50.                         + LocaleContextHolder.getLocale().toString();  
  51.             } else {  
  52.                 urlStr = urlStr + "?locale="  
  53.                         + LocaleContextHolder.getLocale().toString();  
  54.             }  
  55.             url = new URL(urlStr);  
  56.   
  57.             URLConnection uc = url.openConnection();  
  58.             InputStream is = uc.getInputStream();  
  59.               
  60.             Tidy tidy = new Tidy();  
  61.   
  62.             OutputStream os2 = new ByteArrayOutputStream();  
  63.             tidy.setXHTML(true); // 设定输出为xhtml(还可以输出为xml)  
  64.             tidy.setCharEncoding(Configuration.UTF8); // 设定编码以正常转换中文  
  65.             tidy.setTidyMark(false); // 不设置它会在输出的文件中给加条meta信息  
  66.             tidy.setXmlPi(true); // 让它加上<?xml version="1.0"?>  
  67.             tidy.setIndentContent(true); // 缩进,可以省略,只是让格式看起来漂亮一些  
  68.             tidy.parse(is, os2);  
  69.   
  70.             is.close();  
  71.   
  72.             // 解决乱码 --将转换后的输出流重新读取改变编码  
  73.             String temp;  
  74.             StringBuffer sb = new StringBuffer();  
  75.             BufferedReader in = new BufferedReader(new InputStreamReader(  
  76.                     new ByteArrayInputStream(  
  77.                             ((ByteArrayOutputStream) os2).toByteArray()),  
  78.                     "utf-8"));  
  79.             while ((temp = in.readLine()) != null) {  
  80.                 sb.append(temp);  
  81.             }  
  82.   
  83.             return sb.toString();  
  84.         } catch (IOException e) {  
  85.             // logger.error("读取客户端网页文本信息时出错了" + e.getMessage());  
  86.             throw new BaseException(e);  
  87.         }  
  88.   
  89.     }  

 

为了解决包的问题,加上Maven <!-- pdf导出 -->

Xml代码  收藏代码
  1. <dependency>  
  2.     <groupId>com.lowagie</groupId>  
  3.     <artifactId>itext</artifactId>  
  4.     <version>2.1.7</version>  
  5. </dependency>  
  6. <dependency>  
  7.     <groupId>org.xhtmlrenderer.flyingsaucer</groupId>  
  8.     <artifactId>pdf-renderer</artifactId>  
  9.     <version>1.0</version>  
  10. </dependency>  
  11. <dependency>  
  12.     <groupId>jtidy</groupId>  
  13.     <artifactId>jtidy</artifactId>  
  14.     <version>4aug2000r7-dev</version>  
  15.     <type>jar</type>  
  16.     <scope>compile</scope>  
  17. </dependency>  
  18. <dependency>  
  19.     <groupId>net.sf.barcode4j</groupId>  
  20.     <artifactId>barcode4j-light</artifactId>  
  21.     <version>2.0</version>  
  22. </dependency>  
  23. <dependency>  
  24.     <groupId>avalon-framework</groupId>  
  25.     <artifactId>avalon-framework-impl</artifactId>  
  26.     <version>4.2.0</version>  
  27. </dependency>  
  28. <!-- pdf -->  

 

另外附上 稍微复杂的PDFUtils.java文件,由于没时间就不做整理解释了!见下载附件!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics