Well, I was doing some experiment for some stuff but found something interesting to write on my blog. So here goes - "How to generate a PDF through a servlet". I know in all the basic Servlet books you would have studied only "How to generate HTML pages through Servlets" . Rarely any books goes in the depth of the topic. So here is a simple Web application made in Net beans that generates PDF file in the browser.
STEP 1: Open Netbeans[Mine is Netbeans 6.0.1] . Create a simple Web Application Type Project.[Navigation: File ->New Project->Web->Web Application]
STEP 2: Enter "PDFGenerationSampleWebApp" as Project Name and context path as "pdfgen" [Both are user's choice based fields]. Click "Next" tab
STEP 3: Finally "Finish" tab.
STEP 4: Create a new JSP Page "MainPage.jsp" directly in "PDFGenerationSampleWebApp" Project. Note: not in "WEB-INF" folder. [Navigation: "PDFGenerationSampleWebApp" Project -> New File->Web->JSP
STEP 5: Enter following piece of code in it. The page will show a "Generate PDF" button.
STEP 6: Now let's create a Servlet in which we will be writing code for generating PDF through it.
[Navigation: "Source Packages" in Project Structure->New File->Java->Java File
STEP 7: Enter "PDFGeneratorServlet" as JAVA File name and "src" as Package name[ Again, these are user's choice based fields]
Finally click on "Finish" tab. STEP 8: Now enter following code in "PDFGeneratorServlet" JAVA File.
package src;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.PdfPTable;
import com.lowagie.text.pdf.PdfWriter;
import java.io.IOException;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author shalini
*/
public class PDFGeneratorServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException{
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{
doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
/*sets the content type as PDF */
response.setContentType("application/pdf");
Document document = new Document();
try{
/* Servlet Outputstream is passed to generate the PDF */
PdfWriter.getInstance(document, response.getOutputStream());
document.open();
PdfPTable pdfTable = new PdfPTable(2);
pdfTable.addCell("Name");
pdfTable.addCell("Occupation");
pdfTable.addCell("Shalini");
pdfTable.addCell("Software Engineer");
pdfTable.addCell("Crazy");
pdfTable.addCell("Blogger");
document.add(pdfTable);
document.close();
}catch(DocumentException e){
e.printStackTrace();
}
}
}
[Note: There are chances that you may get compile-time errors if i-text.jar file doesn't exist in your Project's Build Path. So , Plese make sure that it is added it. Steps for it are as follows:1. Download it from here Download iText2.17.jar
2. Go to "Library" folder of your Project structure. Right click and select "Add JAR/Library" option.
3. Select iText2.17.jar from your local file system and finally Click on "Open" tab.
]
About .java File:
- response.setContentType("application/pdf") sets the content type of the servlet to PDF. It invokes the PDF Viewer on the client machine to handle the PDF type content.
- In this line PdfWriter.getInstance(document, response.getOutputStream()); ServletOutputStream is passed into PDFWriter to generate PDF.
- PdfPTable table = new PdfPTable(2); A PDF Table is created as the content for the output PDF File.
- document.add(table); Created table is added to the [PDF Type] document.
STEP 8: Now its time to add configuration for "PDFGeneratorServlet.java" File in "web.xml" file.[Navigation: "Configuration Files" folder of Project Structure-> "web.xml".] .It should look like following:
When the "PDFGenerationSampleWebApp" project will be run on the server, "MainPage.jsp" page will open as the welcome page and in MainPage when "Generate PDF" button would be clicked on "MyOwnPDF.pdf" will open up[Look at STEP 5 carefully]. If you see in web.xml carefully, whenever url-pattern is of type "*.pdf", PDFGeneratorServlet class would be called. If you look at the code of PDFGeneratorServlet.java file [Look at STEP 8 carefully] , in doPost() method a PDF is generated and response type of Servlet is set as "application/pdf" . In this way a PDF is generated.
STEP 9: So now finally your entire Project would look something like below:
STEP 10: Now right click the project and "Run" it on server [currently Tomcat is used]. "MainPage.jsp" would open and look something like below
STEP 11: Now click on "Generate PDF" tab and following PDF page would open in the browser itself.
Thanks !












