Java iText PDF Servlet Example Tutorial - Basic HTTPServlet

In this tutorial, we will get back to our iText basics and provide a HTTP Servlet, which will generate a static PDF document and send it back to the browser. This servlet will write a PDF document to the output stream and the web browser can inturn render the PDF document on the page using the Acrobat PDF plugin. The servlet is named as CreatePDFExample and the complete Java code example [ commented, step by step guide ] for this basic PDF servlet using iText is provided below;
/* This Servlet Example Generates a PDF document from a HTML Page using iText Library */
package org.mypackage.PdfExamples;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// Document Object
import com.itextpdf.text.Document;
//For adding content into PDF document
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.DocumentException;

public class CreatePDFExample extends HttpServlet {

        //invoked from doGet method to create PDF through servlet 
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //Set content type to application / pdf
        //browser will open the document only if this is set
        response.setContentType("application/pdf");
        //Get the output stream for writing PDF object        
        OutputStream out=response.getOutputStream();
        try {
            Document document = new Document();
            /* Basic PDF Creation inside servlet */
            PdfWriter.getInstance(document, out);
            document.open();
            document.add(new Paragraph("Tutorial to Generate PDF using Servlet"));
            document.add(new Paragraph("PDF Created Using Servlet, iText Example Works"));
            document.close();
        }
                catch (DocumentException exc){
                throw new IOException(exc.getMessage());
                }
        finally {            
            out.close();
        }
    }
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "This Servlet Generates PDF Using iText Library";
    }
}
This example can be executed from a jsp page after configuring the servlet in your webserver. If you want to know how to setup this servlet in the web.xml file of Tomcat, you can refer to our chart Servlet example using JFreeChart. When you run this example, you should get a PDF back on the browser. If you are caught at any step of this example, let us know.

1 comment:

  1. Could you please provide me with a code that generates a report from a database?

    ReplyDelete