Write data into PDF using Servlet

Write data into PDF using Servlet

In this article, I am going to discuss How to Write data into PDF using Servlet. Please read our previous article where we discussed how to send Email through JavaMail API in Servlet. Here, as part of this article, we will create an application 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 (index.html)
  2. servlet file (Servlet.java)
  3. web.xml file

index.html file provides a link to the servlet through which our PDF content is displayed. Servlet.java file writes data as PDF and provides the information to the server that it is a PDF file type. web.xml is used to configure the servlet file and it provides servlet information to the server.

Note: We need to add the “spdf.jar” jar file for this application. Download this file directly from the Oracle website.

<!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">
  <h1>Click on Below Link to Get your PDF Formatted Text</h1>
  <a href="Servlet">Click Here To See your content</a>
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.darwinsys.spdf.*;
public class Servlet extends HttpServlet {
 private static final long serialVersionUID = 1L;
 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 Formatted"));
  pag.add(new Text(pdf, "This application is created by Manisha"));
  pag.add(new Text(pdf, "Thanks to visit this application"));
<?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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

Run your code to get the following output:

application to Write data into PDF using Servlet

Click on the link, you will find your data in PDF format displayed in the web browser directly.

how to write data into PDF using servlet

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

Write data into PDF using Servlet

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

How to Write data into PDF using Servlet

In the next article, I am going to discuss how to develop a Login Form in Servlet. Here, in this article, we develop an application to Write data into PDF using Servlet and I hope you enjoy this how to write data into PDF using servlet article.

Leave a Reply

Your email address will not be published. Required fields are marked *