java html转pdf

  1. import java.io.ByteArrayInputStream; 
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.InputStream; 
  4. import java.io.OutputStream; 
  5.  
  6. import javax.xml.parsers.DocumentBuilder; 
  7. import javax.xml.parsers.DocumentBuilderFactory; 
  8. import javax.xml.transform.Transformer; 
  9. import javax.xml.transform.TransformerFactory; 
  10. import javax.xml.transform.dom.DOMSource; 
  11. import javax.xml.transform.stream.StreamResult; 
  12.  
  13. import org.w3c.dom.Document; 
  14. import org.xhtmlrenderer.pdf.ITextFontResolver; 
  15. import org.xhtmlrenderer.pdf.ITextRenderer; 
  16.  
  17. public class HTML2PDF { 
  18.  
  19. /**
  20.   * 调 用方式 htmlToPDF(response.getOutputStream(), htmlstr, "C:/WINDOWS/Fonts /simfang.ttf", "FangSong_GB2312", "GB2312", "http://xxxx/dtd/xhtml1-transitional.dtd");
  21.   *
  22.   * 相关图片必须使用绝对路径才能显示 如果要下载生成的文件,在此方法上方加上:
  23.   * response.setHeader("Content-disposition",
  24.   * "attachment;filename=英文文件名.pdf");
  25.   *
  26.   * @param 参数
  27.   * @param OutputStream out 输出流
  28.   * @param String htmlcode html代码。
  29.   *
  30.   * @param String fontName 字体名称 如:FangSong_GB2312
  31.   * @param String fontpath windows 字体路径 如:C:/WINDOWS/Fonts/simfang.ttf
  32.   * @param String encoding 编码 如:GB2312。
  33.   * @param String htmlDTDURL  如:http://xxxx/xhtml1-transitional.dtd
  34.   * version 2009.03.05
  35.   */ 
  36. public void htmlToPDF(OutputStream out, String htmlcode, String fontpath, 
  37.   String 
  38.   fontName, String encoding, String htmlDTDURL) throws Exception { 
  39.   DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 
  40.   ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
  41.   Transformer transformer = TransformerFactory.newInstance() 
  42.     .newTransformer(); 
  43.   ITextRenderer renderer = new ITextRenderer(); 
  44.   ITextFontResolver resolver = renderer.getFontResolver(); 
  45.   InputStream intream = null
  46.   Document doc = null
  47.   Document doc2 = null
  48.   try
  49.   htmlcode = this.filterHeader(htmlcode, 
  50.  
  51.   encoding, fontName, htmlDTDURL); 
  52.    
  53.   intream = new ByteArrayInputStream(htmlcode.getBytes(encoding)); 
  54.   doc = (Document) builder.parse(intream); 
  55.   transformer.setOutputProperty("encoding", encoding); 
  56.   new DOMSource(doc); 
  57.   transformer.transform(new DOMSource(doc), new StreamResult(bos)); 
  58.   intream = new ByteArrayInputStream(bos.toString().getBytes()); 
  59.   doc2 = (Document) builder.parse(intream); 
  60.    
  61.   resolver.addFont(fontpath, BaseFont.IDENTITY_H, 
  62.     BaseFont.NOT_EMBEDDED); 
  63.    
  64.   renderer.setDocument(doc2, null); 
  65.   renderer.layout(); 
  66.    
  67.   renderer.createPDF(out, true); 
  68.    
  69.   out.flush(); 
  70.   out.close(); 
  71.   } catch (Exception ex) { 
  72.    
  73.   throw new Exception(ex.getMessage()); 
  74.   } 
  75. /**
  76.   *文件头
  77. */ 
  78. private String filterHeader(String htmlcode, String encoding,String fontName,String htmlDTDURL) { 
  79.   htmlcode="<table style=\"font-family:FangSong_GB2312\" width=\"100%\" height=\"100%\" border=\"0\"><tr><td>"+htmlcode+"</td></tr></table>"
  80.   htmlcode="<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"  \""+htmlDTDURL+"\" >\n"+htmlcode; 
  81.   htmlcode = "<?xml version=\"1.0\" encoding=\"" + encoding + "\" ?>\n" 
  82.     + htmlcode; 
  83.   return htmlcode; 

你可能感兴趣的:(java html转pdf)