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包裏面的配置文件)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章