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

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

package com.pd4ml.pdf; 

import java.awt.Insets; 
import java.io.File; 
import java.io.FileOutputStream; 
import java.io.StringReader; 

import org.zefer.pd4ml.PD4Constants; 
import org.zefer.pd4ml.PD4ML; 

import com.lowagie.text.FontFactory; 

public class ConverterPdf { 
    public static void main(String[] args) throws Exception { 
        ConverterPdf converter = new ConverterPdf(); 
        converter.generatePDF_2(new File("F:/demo_ch_pd4ml_a.pdf"), "F:/Noname22.html"); 
        File pdfFile = new File("F:/demo_ch_pd4mlssss.pdf"); 
        StringBuffer html = new StringBuffer(); 
        html.append("<html>") 
            .append("<head>") 
            .append("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />") 
            .append("</head>") 
            .append("<body>") 
            .append("<font face=\"KaiTi_GB2312\">") 
            .append("<font color='red' size=22>显示中文</font>") 
            .append("</font>") 
            .append("</body></html>"); 
        StringReader strReader = new StringReader(html.toString()); 
        converter.generatePDF_1(pdfFile, strReader); 
    } 
    // 手动构造HTML代码 
    public void generatePDF_1(File outputPDFFile, StringReader strReader) throws Exception { 
        FileOutputStream fos = new FileOutputStream(outputPDFFile); 
        PD4ML pd4ml = new PD4ML(); 
        pd4ml.setPageInsets(new Insets(20, 10, 10, 10)); 
        pd4ml.setHtmlWidth(950); 
        pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4)); 
        pd4ml.useTTF("java:fonts", true); 
        pd4ml.setDefaultTTFs("SimHei", "Arial", "Courier New"); 
        pd4ml.enableDebugInfo(); 
        pd4ml.render(strReader, fos); 
    } 

    // HTML代码来自于HTML文件 
    public void generatePDF_2(File outputPDFFile, String inputHTMLFileName) throws Exception { 
        FileOutputStream fos = new FileOutputStream(outputPDFFile); 
        PD4ML pd4ml = new PD4ML(); 
        pd4ml.setPageInsets(new Insets(5, 20, 20, 20)); 
        pd4ml.setHtmlWidth(1000); 
        pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4)); 
        pd4ml.useTTF("java:fonts", true); 
        pd4ml.setDefaultTTFs("SimHei", "Arial", "Courier New"); 
        pd4ml.enableDebugInfo(); 
        pd4ml.render("file:" + inputHTMLFileName, fos); 
    } 
} 

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值