pd4ml用来把html生成pdf的实例--需要pd4ml的jar包

package com.blog.testchpdf;

import java.awt.Insets;  
import java.io.File;  
import java.io.IOException;  
import java.net.MalformedURLException;  
import java.net.URL;  
import java.security.InvalidParameterException;  
  
import org.zefer.pd4ml.PD4Constants;  
import org.zefer.pd4ml.PD4ML;  
  
public class GettingStarted1 {  
    protected int topValue = 10;  
    protected int leftValue = 20;  
    protected int rightValue = 10;  
    protected int bottomValue = 10;  
    protected int userSpaceWidth = 1300;  
  
    public static void main(String[] args) {  
        try {  
            GettingStarted1 jt = new GettingStarted1();  
            jt.doConversion("http://pd4ml.com/sample.htm", "c:/pd4ml.pdf");  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
    }  
  
    public void doConversion( String url, String outputPath )   
                throws InvalidParameterException, MalformedURLException, IOException {  
        File output = new File(outputPath);  
        java.io.FileOutputStream fos = new java.io.FileOutputStream(output);  
  
        PD4ML pd4ml = new PD4ML();  
              
        pd4ml.setHtmlWidth(userSpaceWidth); // set frame width of "virtual web browser"   
              
        // choose target paper format and "rotate" it to landscape orientation  
        pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));   
              
        // define PDF page margins  
        pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));   
  
        // source HTML document also may have margins, could be suppressed this way   
        // (PD4ML *Pro* feature):  
        pd4ml.addStyle("BODY {margin: 0}", true);  
              
        // If built-in basic PDF fonts are not sufficient or   
        // if you need to output non-Latin texts,  
        // TTF embedding feature should help (PD4ML *Pro*)  
        pd4ml.useTTF("c:/windows/fonts", true);  
  
        pd4ml.render(new URL(url), fos); // actual document conversion from URL to file  
        fos.close();  
              
        System.out.println( outputPath + "\ndone." );  
    }  
}  

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值