Open In App

Servlet – Write Data into PDF

Last Updated : 16 Feb, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Implementation: 

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.

HTML




<!DOCTYPE html>
<html>
<head>
<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">
</head>
<body bgcolor="cyan">
 <center>
  <h1>Click on Below Link to Get your PDF</h1>
 </center>
 <center>
  <a href="DownloadPdf">Click Here</a>
 </center>
</body>
</html>


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.

Example

Java




import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.*;
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;
    @Override
    protected void doGet(HttpServletRequest request,
                         HttpServletResponse response)
        throws ServletException, IOException
    {
  
        response.setContentType("application/pdf");
  
        response.setHeader(
            "Content-disposition",
            "inline; filename='Downloaded.pdf'");
  
        try {
  
            Document document = new Document();
  
            PdfWriter.getInstance(
                document, response.getOutputStream());
  
            document.open();
  
            document.add(new Paragraph("GeeksforGeeks"));
            document.add(new Paragraph(
                "This is a demo to write data to pdf\n using servlet\nThank You"));
  
            document.close();
        }
        catch (DocumentException de) {
            throw new IOException(de.getMessage());
        }
    }
}


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

XML




<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    
    <servlet>
        <servlet-name>DownloadPdf</servlet-name>
        <servlet-class>DownloadPdf</servlet-class>
    </servlet>
  
    <servlet-mapping>
        <servlet-name>DownloadPdf</servlet-name>
        <url-pattern>/DownloadPdf</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
</web-app>


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.



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads