Convert HTML to PDF with Servlet iText- Java Example - Part1

After providing a HTTP servlet that generated dynamic charts, we will now aim to write a Java Servlet that will accept a HTML string through a HTML page as an input, and then convert the HTML string fragment to a PDF file and return the PDF file back to the browser. This tutorial is essentially an extension of our initial HTML to PDF conversion tutorial using iText, but this time I want to make this conversion dynamic in nature. I would like to pass dynamic html string to the Servlet and get a PDF file each time I pass them, depending on my inputs. I would also like the PDF to contain some XMP Metadata information which I will be passing through a page (covered in later stages). The requirement sounds complex, but with iText and Flying Saucer and with a little knowledge of Java servlet programming, we can crack this in no time. We will begin with writing a servlet and then design our HTML input page and finally blend these two together to see our code in live action. We can get started with this step by step guide now. [ Note: We will use a basic HTML as an example, without CSS and Images. But, using CSS and Images in your HTML file will be explained at a later stage in the tutorial ]

A commented servlet code that converts the HTML to PDF using Java is provided below;
import java.io.IOException;
import java.io.OutputStream;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.xhtmlrenderer.pdf.ITextRenderer;
import com.lowagie.text.DocumentException;
import java.awt.Color;
/* This Servlet accepts a HTML String Fragment and Converts it into PDF file
Using iText and Flying Saucer */
public class HTMLtoPDFServlet extends HttpServlet {
public HTMLtoPDFServlet() {
/* For this example, the constructor does nothing for us */
}
/* We use a doGet method and invoke it internally from a doPost */
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
        OutputStream out = response.getOutputStream(); /* Get the output stream from the response object */
        try {
                /* Set the output response type */
                /* If the below property is not set, the browser will simply dump the PDF as a text file as an output */
                response.setContentType("application/pdf"); /* We have to set this response type for the browser to open the PDF properly */            
                ITextRenderer renderer = new ITextRenderer();
                /* Accept the input provided by user in the HTML form */
                renderer.setDocumentFromString(request.getParameter("InputData"));
                renderer.layout();              
                /* Write the converted PDF output to the output stream */
                renderer.createPDF(out);                
        }
        catch (Exception e) {
                e.printStackTrace(); /* Throw exceptions to log files */
        }
        finally {
                out.close();/* Close the output stream */
        }
        }
/* The doPost method provided below would be invoked when you 
post the data in the HTML form (that contains a HTML string) to
the servlet. With this HTML form, we will invoke doGet and convert the 
HTML string to PDF file */
public void doPost(HttpServletRequest request,
                     HttpServletResponse response)
      throws ServletException, IOException {
    doGet(request, response);
  }     
}
Now, this servlet supports a post method, through which we can obtain the incoming HTML snippet from the FORM data. Once we get this information, we can use iText and Flying Saucer API to write the converted PDF file to the output stream. We have the servlet code on hand, to compile this code, you will need the following JAR files;
servlet-api.jar
core-renderer.jar
core-renderer-minimal.jar
xml-apis-xerces-2.9.1.jar
iText-2.0.8.jar
You can also use higher versions if available, but make sure that you change your import declarations accordingly. In the next part of this post, we will see how to setup this servlet in Tomcat and create a HTML page and generate dynamic PDF on the fly.

1 comment:

  1. Have provided your example as an answer here http://stackoverflow.com/a/12211074/468763

    Thanks for this post.

    ReplyDelete