public static void convertHtml2Pdf(String htmlPath, String pdfPath) throws FileNotFoundException, IOException, com.lowagie.text.DocumentException {
final ITextRenderer iTextRenderer = new ITextRenderer();
iTextRenderer.setDocument(htmlPath);
iTextRenderer.layout();
/** The generated pdf will be written to the file. */
final FileOutputStream fileOutputStream =
new FileOutputStream(new File(pdfPath));
/** Creating the pdf */
iTextRenderer.createPDF(fileOutputStream);
fileOutputStream.close();
}
1 回答
您可以使用iTextPdf库或飞碟(它依次使用iText库) . 我更喜欢Flying Saucer,因为它可以将几乎所有的css样式从html转换为pdf,而iTextPdf在css兼容方面非常有限 . 这是我用飞碟将HTML转换为PDF的静态方法: