使用itext将html转换为pdf

package test;

/*
 *     private static final String DEST = "C:/Users/钟倩文/Desktop/pdf测试文件/HelloWorld_CN_HTML.pdf";
    private static final String HTML = "D:/毕业设计/pdfBox/test/src/html/test.html";
    private static final String FONT = "D:/毕业设计/pdfBox/test/src/resources/simhei.ttf";
 * */


import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
  
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
  
/**
 * Created by lujianing on 2017/5/7.
 */
public class test1 {
  
		private static final String DEST = "C:/Users/钟倩文/Desktop/pdf测试文件/HelloWorld_CN_HTML.pdf";
	    private static final String HTML = "D:/毕业设计/pdfBox/test/src/html/test.html";
	    private static final String FONT = "D:/毕业设计/pdfBox/test/src/resources/simhei.ttf";
  
  
    public static void main(String[] args) throws IOException, DocumentException {
        // step 1
        Document document = new Document();
        // step 2
        PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(DEST));
        // step 3
        document.open();
        // step 4
        XMLWorkerFontProvider fontImp = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
        fontImp.register(FONT);
        XMLWorkerHelper.getInstance().parseXHtml(writer, document,
                new FileInputStream(HTML), null, Charset.forName("UTF-8"), fontImp);
        // step 5
        document.close();
    }
}

你可能感兴趣的:(毕设)