Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App

Related Articles

Servlet – Write Data into PDF

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

A Servlet is an object that accepts a request and responds to it with a response. The basic Servlet package contains Java objects that represent servlet requests and responses, as well as configuration and execution environment information. Servlets may react to any form of request, although they are most typically used to expand web server-hosted applications. Java For such applications, servlet technology provides HTTP-specific servlet classes.

Let’s see how to write data into PDF using servlet technology. Here simply a servlet is utilized to write some data, which will then be shown as a PDF. An application is created as part of this article that outputs the data to a PDF file. 

Here NetBeans IDE is used for creating this demo application.

in order to create the Application, the following files are required:

  • HTML File – This file contains a URL to the servlet that displays our PDF material.
  • Servlet File – This file saves data as a PDF file and informs the server that it is a PDF file type.
  • web.xml – It’s used to set up the servlet file. The server receives servlet information from this XML file.


Let’s get started building this application.

Step 1: The first step is to open NetBeans IDE and create a new project.

Step 2: Now Select “Java web” -> “Web application” as shown below.

Step 3: Name the project as below.

Step 4: Choose the version and server wizard and click finish.

Step 5: Now copy the code in index.html provided below.


<!DOCTYPE html>
<title>Write Data into Pdf using Servlet</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width">
<body bgcolor="cyan">
  <h1>Click on Below Link to Get your PDF</h1>
  <a href="DownloadPdf">Click Here</a>

Step 6: Download and Add the “itextpdf.jar” file to your project library.

Step 7: Now create a servlet named “DownloadPdf” and copy the provided code into it.



import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.util.Date;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class DownloadPdf extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
        throws ServletException, IOException
            "inline; filename='Downloaded.pdf'");
        try {
            Document document = new Document();
                document, response.getOutputStream());
            document.add(new Paragraph("GeeksforGeeks"));
            document.add(new Paragraph(
                "This is a demo to write data to pdf\n using servlet\nThank You"));
        catch (DocumentException de) {
            throw new IOException(de.getMessage());

Step 8: Make sure your “web.xml” file is identical to the code below.


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="" 

Step 9: The project is ready to run and the output for the Application is as shown below.

Step 10: After clicking on the link a pdf file will begin to download.

Step 11: The downloaded file shows the output as shown below.

My Personal Notes arrow_drop_up
Last Updated : 16 Feb, 2022
Like Article
Save Article
Similar Reads
Related Tutorials