<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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) 閱讀(1429) 評論(0)  編輯  收藏

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


    網(wǎng)站導(dǎo)航:
     
    主站蜘蛛池模板: 99久久免费观看| 免费国产高清毛不卡片基地| 日韩精品一区二区亚洲AV观看 | 免费A级毛片无码视频| 中文字幕不卡高清免费| 一级毛片在线播放免费| 免费人人潮人人爽一区二区| 国产精品无码亚洲精品2021| 国产精品亚洲av色欲三区| 久久久亚洲精华液精华液精华液| 亚洲日韩国产欧美一区二区三区| 亚洲成a人不卡在线观看| 久久久久亚洲av无码专区导航| 久久亚洲AV成人无码| 99热这里只有精品6免费| 国产精品免费AV片在线观看| 久久狠狠躁免费观看| 99热这里有免费国产精品| AV大片在线无码永久免费| 成年美女黄网站色大免费视频| 日本二区免费一片黄2019| 亚洲国产成人精品久久久国产成人一区二区三区综| 四虎影视精品永久免费| 久久亚洲欧洲国产综合| 亚洲人成电影福利在线播放 | 亚洲av无码潮喷在线观看| 亚洲综合激情视频| 中文字幕亚洲男人的天堂网络 | 久久久久亚洲Av无码专| 在线综合亚洲中文精品| 在线亚洲精品视频| 中文字幕乱码免费看电影| 1000部拍拍拍18勿入免费凤凰福利| 永久免费的网站在线观看| 国产午夜影视大全免费观看 | 成人av免费电影| 国产亚洲精品无码专区| 亚洲黄色在线网站| 激情小说亚洲色图| 暖暖免费在线中文日本| 一二三四在线观看免费高清中文在线观看|