160603、使用pd4ml.jar和ss_css2.jar轉pdf的工具類


注意:需要導入pd4ml.jar和ss_css2.jar

import java.awt.Insets;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.InvalidParameterException;
import org.zefer.pd4ml.PD4Constants;
import org.zefer.pd4ml.PD4ML;
/***
 * pd4ml.jar,ss_css2.jar
 * @author admin
 */
public class ToPdfUtils {
    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) throws InvalidParameterException, MalformedURLException, IOException {
        ToPdfUtils pdfUtils = new ToPdfUtils();
//        pdfUtils.doConversion("http://pd4ml.com/sample.htm", "g:/test/pd4ml.pdf");//網絡地址
        String html = readFile("g:/test/confirm.html", "UTF-8");  //文件地址
        pdfUtils.doConversion2(html, "g:/test/pd4ml2.pdf");  
    }
    
    /**將文件轉換成pdf,源文件為http://開頭的網絡地址*/
    public void doConversion(String url, String outputPath)
            throws InvalidParameterException, MalformedURLException,
            IOException {
        File output = new File(outputPath);
        FileOutputStream fos = new FileOutputStream(output);
        PD4ML pd4ml = new PD4ML();
        pd4ml.setHtmlWidth(userSpaceWidth);
        pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));
        pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue,rightValue));
        pd4ml.addStyle("BODY {margin: 0}", true);
        pd4ml.useTTF("java:fonts", true);//src下fonts文件夾中的字體設置
        pd4ml.render(new URL(url), fos);
        fos.close();
        System.out.println(outputPath + "\ndone.");
    }
    
    public void doConversion2( String htmlDocument, String outputPath )   
            throws InvalidParameterException, MalformedURLException, IOException {  
        PD4ML pd4ml = new PD4ML();  
        pd4ml.setHtmlWidth(userSpaceWidth);
        // 選擇目標文件的格式
        pd4ml.setPageSize(pd4ml.changePageOrientation(PD4Constants.A4));   
        // 設置邊距  
        pd4ml.setPageInsetsMM(new Insets(topValue, leftValue, bottomValue, rightValue));   
        //原來的html文檔也有邊距,可以通過這個方式壓縮
        pd4ml.addStyle("BODY {margin: 0}", true);  
        // 如果內置的基本pdf字體不夠用,可以設置成non-Latin,TTF能夠做到這一點
        pd4ml.useTTF("java:fonts", true);  
        ByteArrayOutputStream baos = new ByteArrayOutputStream();  
        pd4ml.render(new StringReader(htmlDocument), baos);   
        baos.close();  
        File output = new File(outputPath);  
        FileOutputStream fos = new FileOutputStream(output);  
        fos.write( baos.toByteArray() );  
        fos.close();  
        System.out.println( outputPath + "\ndone." );  
    }  
 
    private final static String readFile( String path, String encoding ) throws IOException {  
        File f = new File( path );  
        FileInputStream is = new FileInputStream(f);  
        BufferedInputStream bis = new BufferedInputStream(is);  
        ByteArrayOutputStream fos = new ByteArrayOutputStream();  
        byte buffer[] = new byte[2048];  
        int read;  
        do {  
            read = is.read(buffer, 0, buffer.length);  
            if (read > 0) {   
                fos.write(buffer, 0, read);   
            }  
        } while (read > -1);  
        fos.close();  
        bis.close();  
        is.close();  
        return fos.toString(encoding);  
    }  
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM