html转pdf pd4ml

    直接把html代码转化成pdf文件,不需要调用插件

    直接上代码

    

public void Conversion(File file , String outputPath ) throws Exception{
    int topValue = 10;  
    int leftValue = 10;  
    int rightValue = 10;  
    int bottomValue = 10;  
    int userSpaceWidth = 800; 
    PD4ML pd4ml = new PD4ML();
    
    pd4ml.setHtmlWidth(userSpaceWidth);
  
    pd4ml.setPageSize(PD4Constants.A4);//pd4ml.changePageOrientation()
  
    pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));
  
    //pd4ml.addStyle("BODY {margin: 500}", true);
  
    pd4ml.useTTF("java:fonts", true);
    pd4ml.setDefaultTTFs("SimHei", "SimHei", "SimHei"); 
    pd4ml.enableDebugInfo();
    FileInputStream in = new FileInputStream(file);
    InputStreamReader is = new InputStreamReader(in);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    pd4ml.render(is, baos);
    baos.close();  
  
    System.out.println( "resulting PDF size: " + baos.size() + " bytes" );  
  
    File output = new File(outputPath);
    FileOutputStream fos = new FileOutputStream(output);  
      fos.write( baos.toByteArray() );
      fos.close();  
  
      System.out.println( outputPath + "\ndone." );
  }

    只需要传入源文件,以后输出路径即可,需要的jar:pd4ml.jar; font.jar; ss_css2.jar; 点击下载

    (font.jar一般不支持中文字体,需要自己修改jar包里面的配置文件)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值