2013-10-21 91 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(); 
}