2013-10-21 152 views
0

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

+0

看到这个。您的问题是重复的 http://stackoverflow.com/questions/17825782/how-to-convert-html-to-pdf-using-itext – AJJ

+0

可能重复[转换HTML文件为PDF](http:// stackoverflow .COM /问题/ 633780 /转换-HTML的文件到PDF) – Autar

回答

0

您可以使用iTextPdf库或飞碟(反过来使用iText库)。我更喜欢Flying Saucer,因为它可以将几乎所有的CSS样式从html转换为pdf,而iTextPdf在css兼容方面非常有限。这里是我使用Flying saucer将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(); 
}