pingpang

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  21 Posts :: 0 Stories :: 3 Comments :: 0 Trackbacks

网上有好几种方法可以将将HTML文件转换成PDF文件但是有些对HTML文件格式要求比较严格,稍微错了一些就不能生成我们所要的PDF文件,这里我推荐一个

PD4ML,它可以解决HTML文件格式不正确的问题,可以生成一个比较好的PDF文件,其处理速度快,而且对CSS文件兼容的非常好。下面是最基本的

PD4ML编程:

Java代码 
  1. package samples;  
  2.   
  3. import java.awt.Insets;  
  4. import java.io.File;  
  5. import java.io.IOException;  
  6. import java.net.MalformedURLException;  
  7. import java.net.URL;  
  8. import java.security.InvalidParameterException;  
  9.   
  10. import org.zefer.pd4ml.PD4Constants;  
  11. import org.zefer.pd4ml.PD4ML;  
  12.   
  13. public class GettingStarted1 {  
  14.     protected int topValue = 10;  
  15.     protected int leftValue = 20;  
  16.     protected int rightValue = 10;  
  17.     protected int bottomValue = 10;  
  18.     protected int userSpaceWidth = 1300;  
  19.   
  20.     public static void main(String[] args) {  
  21.         try {  
  22.             GettingStarted1 jt = new GettingStarted1();  
  23.             jt.doConversion("http://pd4ml.com/sample.htm", "c:/pd4ml.pdf");  
  24.         } catch (Exception e) {  
  25.             e.printStackTrace();  
  26.         }  
  27.     }  
  28.   
  29.     public void doConversion( String url, String outputPath )   
  30.                 throws InvalidParameterException, MalformedURLException, IOException {  
  31.         File output = new File(outputPath);  
  32.         java.io.FileOutputStream fos = new java.io.FileOutputStream(output);  
  33.   
  34.         PD4ML pd4ml = new PD4ML();  
  35.               
  36.         pd4ml.setHtmlWidth(userSpaceWidth); // set frame width of "virtual web browser"   
  37.               
  38.         // choose target paper format and "rotate" it to landscape orientation  
  39.         pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));   
  40.               
  41.         // define PDF page margins  
  42.         pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));   
  43.   
  44.         // source HTML document also may have margins, could be suppressed this way   
  45.         // (PD4ML *Pro* feature):  
  46.         pd4ml.addStyle("BODY {margin: 0}", true);  
  47.               
  48.         // If built-in basic PDF fonts are not sufficient or   
  49.         // if you need to output non-Latin texts,  
  50.         // TTF embedding feature should help (PD4ML *Pro*)  
  51.         pd4ml.useTTF("c:/windows/fonts", true);  
  52.   
  53.         pd4ml.render(new URL(url), fos); // actual document conversion from URL to file  
  54.         fos.close();  
  55.               
  56.         System.out.println( outputPath + "\ndone." );  
  57.     }  
  58. }  
The following Java class slightly changes the above example. Now it pre-reads source HTML to a string and passes it torender()method wrapped toStringReader. First it writes PDF bytes toByteArrayOutputStream, which makes possible to measure size of the resulting document.

A disadvantage of the method is a bigger RAM utilization.

Java代码 
  1. package samples;  
  2.   
  3. import java.awt.Insets;;  
  4. import java.io.BufferedInputStream;  
  5. import java.io.ByteArrayOutputStream;  
  6. import java.io.File;  
  7. import java.io.FileInputStream;  
  8. import java.io.FileOutputStream;  
  9. import java.io.IOException;  
  10. import java.io.StringReader;  
  11. import java.net.MalformedURLException;  
  12. import java.net.URL;  
  13. import java.security.InvalidParameterException;  
  14.   
  15. import org.zefer.pd4ml.PD4Constants;  
  16. import org.zefer.pd4ml.PD4ML;  
  17.   
  18. public class GettingStarted2 {  
  19.     protected int topValue = 10;  
  20.     protected int leftValue = 20;  
  21.     protected int rightValue = 10;  
  22.     protected int bottomValue = 10;  
  23.     protected int userSpaceWidth = 1300;  
  24.   
  25.     public static void main(String[] args) {  
  26.         try {  
  27.             GettingStarted2 jt = new GettingStarted2();  
  28.             String html = readFile("c:/sample.htm", "UTF-8");  
  29.             jt.doConversion2(html, "c:/pd4ml.pdf");  
  30.         } catch (Exception e) {  
  31.             e.printStackTrace();  
  32.         }  
  33.     }  
  34.   
  35.     public void doConversion2( String htmlDocument, String outputPath )   
  36.                 throws InvalidParameterException, MalformedURLException, IOException {  
  37.   
  38.         PD4ML pd4ml = new PD4ML();  
  39.               
  40.         pd4ml.setHtmlWidth(userSpaceWidth); // set frame width of "virtual web browser"   
  41.               
  42.         // choose target paper format  
  43.         pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));   
  44.               
  45.         // define PDF page margins  
  46.         pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));   
  47.   
  48.         // source HTML document also may have margins, could be suppressed this way   
  49.         // (PD4ML *Pro* feature):  
  50.         pd4ml.addStyle("BODY {margin: 0}", true);  
  51.               
  52.         // If built-in basic PDF fonts are not sufficient or   
  53.         // if you need to output non-Latin texts, TTF embedding feature should help   
  54.         // (PD4ML *Pro*)  
  55.         pd4ml.useTTF("c:/windows/fonts", true);  
  56.   
  57.         ByteArrayOutputStream baos = new ByteArrayOutputStream();  
  58.         // actual document conversion from HTML string to byte array  
  59.         pd4ml.render(new StringReader(htmlDocument), baos);   
  60.         // if the HTML has relative references to images etc,   
  61.         // use render() method with baseDirectory parameter instead  
  62.         baos.close();  
  63.           
  64.         System.out.println( "resulting PDF size: " + baos.size() + " bytes" );  
  65.         // in Web scenarios it is a good idea to send the size with   
  66.         // "Content-length" HTTP header  
  67.   
  68.         File output = new File(outputPath);  
  69.         java.io.FileOutputStream fos = new java.io.FileOutputStream(output);  
  70.         fos.write( baos.toByteArray() );  
  71.         fos.close();  
  72.           
  73.         System.out.println( outputPath + "\ndone." );  
  74.     }  
  75.       
  76.     private final static String readFile( String path, String encoding ) throws IOException {  
  77.   
  78.         File f = new File( path );  
  79.         FileInputStream is = new FileInputStream(f);  
  80.         BufferedInputStream bis = new BufferedInputStream(is);  
  81.           
  82.         ByteArrayOutputStream fos = new ByteArrayOutputStream();  
  83.         byte buffer[] = new byte[2048];  
  84.   
  85.         int read;  
  86.         do {  
  87.             read = is.read(buffer, 0, buffer.length);  
  88.             if (read > 0) {   
  89.                 fos.write(buffer, 0, read);   
  90.             }  
  91.         } while (read > -1);  
  92.   
  93.         fos.close();  
  94.         bis.close();  
  95.         is.close();  
  96.   
  97.         return fos.toString(encoding);  
  98.     }  
  99. }  
posted on 2012-07-21 22:19 往事随风 阅读(1415) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: