【转】 java生成pdf方案总结

来源:互联网 发布:java http 发送文件 编辑:程序博客网 时间:2024/05/20 11:52

java生成pdf方案总结

文章分类:Java编程

java生成pdf方案很多,常用的如下:

1. 利用jacob生成pdf:这种方法调用office的本地方法实现对pdf API的操作,只能在windows平台使用

2. 利用openoffice生成pdf:openoffice是开源软件且能在windows和linux平台下运行

3. itext + flying saucer生成pdf:itext和flying saucer都是免费开源的,且与平台无关,结合css和velocity技术,可以很好的实现。

我们重点介绍第三种方案。它实现的步骤是非常简单的:
1.新建一个ITextRenderer类
2.添加字体
3.设置ITextRenderer的源文档
4.调用layout()方法
5.调用createPdf()方法
6.关闭输出流

代码如下:
Java代码 复制代码
  1. package com.hank.pdfhtml;   
  2.   
  3. /**  
  4.  * @author Hank  
  5.  * 2009-12-30  
  6.  */  
  7.   
  8. import java.io.File;   
  9. import java.io.FileNotFoundException;   
  10. import java.io.FileOutputStream;   
  11. import java.io.IOException;   
  12. import java.io.OutputStream;   
  13. import java.net.MalformedURLException;   
  14. import java.net.URL;   
  15.   
  16. import org.xhtmlrenderer.pdf.ITextFontResolver;   
  17. import org.xhtmlrenderer.pdf.ITextRenderer;   
  18.   
  19. import com.lowagie.text.DocumentException;   
  20. import com.lowagie.text.pdf.BaseFont;   
  21.   
  22. public class Html2Pdf {   
  23.     private static void addFonts() throws DocumentException, IOException{   
  24.         if(null == renderer) {   
  25.             return;   
  26.         }   
  27.            
  28.         // 添加所需的字体   
  29.         ITextFontResolver fontResolver = renderer.getFontResolver();    
  30.   
  31.         URL fontsUrl = Html2Pdf.class.getResource("/com/hank/fonts/");//该文件夹下放所需字体文件   
  32.         File fonts = new File(fontsUrl.getPath());   
  33.         File[] fileList = fonts.listFiles();   
  34.         for(int i=0; i < fileList.length; i++){   
  35.             fontResolver.addFont(fileList[i].getAbsolutePath(), BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);   
  36.         }   
  37.            
  38.     }   
  39.        
  40.     public static String print2Pdf(String inputFile) {   
  41.         String url = null;   
  42.         try {   
  43.             url = new File(inputFile).toURI().toURL().toString();   
  44.         } catch (MalformedURLException e) {   
  45.             return null;   
  46.         }   
  47.   
  48.   
  49.         String outputFile = inputFile.substring(0, inputFile.lastIndexOf(".")) + ".pdf";   
  50.   
  51.         OutputStream os = null;   
  52.         try {   
  53.             os = new FileOutputStream(outputFile);   
  54.         } catch (FileNotFoundException e) {   
  55.                
  56.             return null;   
  57.         }   
  58.   
  59.         ITextRenderer renderer = null;   
  60.         try {   
  61.             renderer = new ITextRenderer();   
  62.         } catch (Exception e) {   
  63.             return null;   
  64.         }   
  65.            
  66.         renderer.setDocument(url);   
  67.            
  68.         // 解决图片的相对路径问题   
  69.         renderer.getSharedContext().setBaseURL("file:/D:/working/HtmlTemp/image/");   
  70.            
  71.         renderer.layout();   
  72.         try {   
  73.             renderer.createPDF(os);   
  74.         } catch (DocumentException e) {   
  75.             return null;   
  76.         }   
  77.            
  78.         try {   
  79.             os.close();   
  80.         } catch (IOException e) {   
  81.             return null;   
  82.         }   
  83.            
  84.         return outputFile;   
  85.     }   
  86.   
  87.         public static void main(String args[]){   
  88.             String inputFile = "D:/working/HtmlTemp/test.html"//必须符合W3C标准   
  89.             Html2Pdf.print2Pdf(inputFile);   
  90.         }   
  91. }  
原创粉丝点击