pingpang

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            21 Posts :: 0 Stories :: 3 Comments :: 0 Trackbacks

          網(wǎng)上有好幾種方法可以將將HTML文件轉(zhuǎn)換成PDF文件但是有些對HTML文件格式要求比較嚴(yán)格,稍微錯了一些就不能生成我們所要的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 往事隨風(fēng) 閱讀(1439) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 磐安县| 新疆| 昭苏县| 凤冈县| 灌阳县| 博罗县| 孟村| 曲阜市| 盖州市| 淮安市| 永定县| 平乐县| 信丰县| 义乌市| 通化市| 九江市| 海伦市| 迁西县| 民乐县| 富川| 丁青县| 祁东县| 库伦旗| 那曲县| 石首市| 宣化县| 丹巴县| 崇礼县| 佛冈县| 沁源县| 宜宾县| 克什克腾旗| 新竹市| 蓬莱市| 文昌市| 怀柔区| 迁西县| 邯郸县| 天津市| 卢龙县| 斗六市|