pdf生成


pd4ml技术html导出pdf,支持中文,兼容Linux

javapd4ml .


     java html导出pdf的文章有很多大多都使用的是itext,其实用过的都知道itext有时并不能满足我们的需求,不能兼容html的样式,而且从html页面导出的图片到pdf中也并不好处理。Flying Sauser实现html2pdf,纠错能力差,支持多种中文字体(部分样式不能识别),而且对html的格式也是十分的严格,如果使用一种模版的话使用Flying Sauser技术倒是不错的选择,但是对于不规则的html导出pdf就并不是那么的适用。这时我们就要考虑使用其他的技术,而PD4ML可以满足我们需求,PD4ML实现html2pdf,速度快,纠错能力强可以过滤不规则的html标记,支持多种中文字体,支持css。



Html代码 
1.package com.pd4ml.pdf;  
2. 
3.import java.awt.Insets;  
4.import java.io.File;  
5.import java.io.FileOutputStream;  
6.import java.io.StringReader;  
7. 
8.import org.zefer.pd4ml.PD4Constants;  
9.import org.zefer.pd4ml.PD4ML;  
10. 
11.import com.lowagie.text.FontFactory;  
12. 
13.public class ConverterPdf {  
14.    public static void main(String[] args) throws Exception {  
15.        ConverterPdf converter = new ConverterPdf();  
16.        converter.generatePDF_2(new File("F:/demo_ch_pd4ml_a.pdf"), "F:/Noname22.html");  
17.        File pdfFile = new File("F:/demo_ch_pd4mlssss.pdf");  
18.        StringBuffer html = new StringBuffer();  
19.        html.append("<html>")  
20.            .append("<head>")  
21.            .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />")  
22.            .append("</head>")  
23.            .append("<body>")  
24.            .append("<font face=\"KaiTi_GB2312\">")  
25.            .append("<font color='red' size=22>显示中文</font>")  
26.            .append("</font>")  
27.            .append("</body></html>");  
28.        StringReader strReader = new StringReader(html.toString());  
29.        converter.generatePDF_1(pdfFile, strReader);  
30.    }  
31.    // 手动构造HTML代码  
32.    public void generatePDF_1(File outputPDFFile, StringReader strReader) throws Exception {  
33.        FileOutputStream fos = new FileOutputStream(outputPDFFile);  
34.        PD4ML pd4ml = new PD4ML();  
35.        pd4ml.setPageInsets(new Insets(20, 10, 10, 10));  
36.        pd4ml.setHtmlWidth(950);  
37.        pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));  
38.        pd4ml.useTTF("java:fonts", true);  
39.        pd4ml.setDefaultTTFs("SimHei", "Arial", "Courier New");  
40.        pd4ml.enableDebugInfo();  
41.        pd4ml.render(strReader, fos);  
42.    }  
43. 
44.    // HTML代码来自于HTML文件  
45.    public void generatePDF_2(File outputPDFFile, String inputHTMLFileName) throws Exception {  
46.        FileOutputStream fos = new FileOutputStream(outputPDFFile);  
47.        PD4ML pd4ml = new PD4ML();  
48.        pd4ml.setPageInsets(new Insets(5, 20, 20, 20));  
49.        pd4ml.setHtmlWidth(1000);  
50.        pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));  
51.        pd4ml.useTTF("java:fonts", true);  
52.        pd4ml.setDefaultTTFs("SimHei", "Arial", "Courier New");  
53.        pd4ml.enableDebugInfo();  
54.        pd4ml.render("file:" + inputHTMLFileName, fos);  
55.    }  
56.}  

附件中有源码,用到了pd4ml.jar,ss_css2.jar,fonts.jar(包太大全部文件在我的csdn上)下载地址:http://download.csdn.net/detail/sy456zsc/4480028


猜你喜欢

转载自jin-deng.iteye.com/blog/1872003