How to Write Data in PDF Format Using Servlet in Java


This article explains how to write data in PDF format using a servlet in Java. The NetBeans IDE is used for creating this application.


For this application you need a jar file named "spdf.jar". You can directly download this file from the Oracle web site.

How to write data in PDF file format

In this article we create an app that writes our data in PDF file format. We are writing some data in PDF using a servlet program and it will be displayed in the PDF file format.

To create this application, you need to create the following files:

  1. HTML file
  2. servlet file
  3. web.xml file

1. HTML File

This file provides a link to the servlet through which our PDF content is displayed.

2. Servlet File

This file writes data as PDF and provides the information to the server that it is a PDF file type.

3. web.xml file

Used to configure the servlet file. This XML file provides servlet information to the server.

Let's start creating this application

For this application we need to use the following procedure.

NetBeans IDE

Open the NetBeans ide.


Step 2

Choose "Java web" -> "Web application" as in the following.

Choose java web application

Step 3

Type your project names as "PDFApp" as in the following.

PDFApp project

Step 4

Choose your version and server wizard as in the following.

choose server and version wizard

Step 5

Now delete your default "index.html" file and create a new "index.html" file and provide the following for it:


<!DOCTYPE html>



        <title>TODO supply a title</title>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

        <meta name="viewport" content="width=device-width">


    <body bgcolor="pink">


            Click on Below Link to Get your PDF Formatted Text


        <a href="PDFServlet">Click Here To See your content</a>



Step 6

Create a servlet named "PDFServlet" and and provide the following for it:

import java.util.*;


import javax.servlet.*;

import javax.servlet.http.*;

import com.darwinsys.spdf.*;

public class PDFServlet extends HttpServlet {



    protected void doGet(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {


        PrintWriter out=response.getWriter();

        response.setHeader("Content-disposition","inline; filename='Downloaded.pdf'");

        PDF pdf=new PDF(out);

        Page pag=new Page(pdf);

        pag.add(new MoveTo(pdf,150,730));

        pag.add(new Text(pdf,"Hello User"));

        pag.add(new Text(pdf,"This is a content of PDF Formated"));

        pag.add(new Text(pdf,"This application is created by Sandeep"));

        pag.add(new Text(pdf,"Thanks to visit this application"));







Add the "spdf.jar" file to your project library.

Step 7

Check your "web.xml" file that it is the same as in the following code.


<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="" xmlns:xsi="" xsi:schemaLocation="">















Step 8

Now your project is ready to run.

Right-click on the project menu then select "Run". The following output is generated.


Step 9

Click on the given link; you'll find your data (that you passed in your servlet code) in PDF format displayed in the web browser directly as shown below.

Data in PDF view

Step 10

If you want to se your page format then you just save this page and you see it in PDF format.

Downlaod file

Step 11

Open this downloaded file, you get the file in PDF format as shown below.


Up Next
    Ebook Download
    View all
    View all