Tuesday, 9 June 2015

How to convert HTML to PDF using iText

I got a new task today to generate a PDF out of HTML files. After doing some analysis on this, I found that there are couple of ways to achieve this.
1) You can convert the HTML to XSL-FO format and then generate the PDF using Apache XSL-FO
2) Generate PDF using iText libraries.

We are going to see how to generate PDF using iText lib. It can be achieved by using two different classes.

1) HTMLWorker (com.itextpdf.text.html.simpleparser.HTMLWorker) - Deprecated since v-5.5.2 - Java Doc

try {
    String k = "<html><body> Generate PDF using HTMLWorker Class </body></html>";
    OutputStream file = new FileOutputStream(new File("C:\\HtmlWorker.pdf"));
    Document document = new Document();
    PdfWriter.getInstance(document, file);
    document.open();
    HTMLWorker htmlWorker = new HTMLWorker(document);
    htmlWorker.parse(new StringReader(k));
    document.close();
    file.close();
} catch (Exception e) {
    e.printStackTrace();
}


2)XMLWorker (com.itextpdf.tool.xml.XMLWorker)

try {
    String k = "<html><body> Generate PDF using XML Worker</body></html>";
    OutputStream file = new FileOutputStream(new File("C:\\XMLWorker.pdf"));
    Document document = new Document();
    PdfWriter writer = PdfWriter.getInstance(document, file);
    document.open();
    InputStream is = new ByteArrayInputStream(k.getBytes());
    XMLWorkerHelper.getInstance().parseXHtml(writer, document, is);
    document.close();
    file.close();
} catch (Exception e) {
    e.printStackTrace();
}

No comments:

Post a Comment