首页 文章

使用java中的itext将html转换为pdf

提问于
浏览
0

将html文件转换为pdf文件 . 我有html文件,css文件和js文件在一个文件夹中如何转换index.html使用java中的itext创建pdf . 任何人都可以帮我解决这个问题 . 是否有任何示例项目?

1 回答

  • 0

    您可以使用iTextPdf库或飞碟(它依次使用iText库) . 我更喜欢Flying Saucer,因为它可以将几乎所有的css样式从html转换为pdf,而iTextPdf在css兼容方面非常有限 . 这是我用飞碟将HTML转换为PDF的静态方法:

    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();
    }
    

相关问题