Thursday, August 1, 2013

Write your HTML in PDF

Many of the websites are so scool that they allow the content to be downloaded as PDF. You can also make your website that cool. I am using iText for the same. It is available under AGFL. So, if you are making your source code distribution under AGPL or greater public reach than AGPL, then iText should be your choice. Its easy and convenient. Many HTML tags are also supported.

Only external images are not supported and hyperlink did not work for me. Apart from these drawbacks, everything just worked fine. I'll share one input and output from my web page.

Dependencies you need to have,
  • itext-pdfa-5.5.4.jar
  • itext-xtra-5.5.4.jar
  • itextpdf-5.5.4.jar
These dependencies are available here. Choose according to your need.

Program you need to write to create PDF,

 import java.io.File;  
 import java.io.FileNotFoundException;  
 import java.io.FileOutputStream;  
 import java.io.IOException;  
 import java.io.OutputStream;  
 import java.io.StringReader;  
 import com.itextpdf.text.Document;  
 import com.itextpdf.text.DocumentException;  
 import com.itextpdf.text.html.simpleparser.HTMLWorker;  
 import com.itextpdf.text.pdf.PdfWriter;  
 public class PDFCreator {  
      /**  
       *   
       * @param args  
       * @throws DocumentException  
       * @throws IOException  
       */  
      public static void main(String[] args) throws DocumentException,  
                IOException {  
           generatePDF(  
                     "/pdf.pdf",  
                     "<html><head><title>Title</title></head><body><p>Paragraph Tag</p><h1>First Level Header</h1><h2>Second Level Header</h2></body></html>");  
      }  
      /**  
       * Generates HTML with different HTML tags. CSS also supported  
       *   
       * @param filePath  
       * @param htmlData  
       * @throws FileNotFoundException  
       * @throws DocumentException  
       * @throws IOException  
       */  
      public static void generatePDF(String filePath, String htmlData)  
                throws FileNotFoundException, DocumentException, IOException {  
           Document document = new Document();  
           OutputStream output = new FileOutputStream(new File(filePath));  
           PdfWriter.getInstance(document, output);  
           document.open();  
           HTMLWorker htmlWorker = new HTMLWorker(document);  
           htmlWorker.parse(new StringReader(htmlData));  
           document.close();  
           output.close();  
      }  
 }  

That's it. Just use it in your code and you are good to go !!!

Sample from my webpage,

Input text in Editor
This was the input in the web page. Now, I have converted it in a PDF file. The output looks like below,

Output in PDF
That's it. Enjoy writing to PDF from HTML tags !!!
Palash Kanti Kundu