Back to: Java Servlets Tutorials
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:
- HTML file (index.html)
- servlet file (Servlet.java)
- 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.
index.html
<!DOCTYPE html> <html> <head> <title>TODO supply a title</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta name="viewport" content="width=device-width"> </head> <body bgcolor="pink"> <center> <h1>Click on Below Link to Get your PDF Formatted Text</h1> <a href="Servlet">Click Here To See your content</a> </body> </html>
Servlet.java
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; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/pdf"); 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")); pdf.add(pag); pdf.writePDF(); out.close(); } }
web.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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <servlet> <servlet-name>Servlet</servlet-name> <servlet-class>Servlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>Servlet</servlet-name> <url-pattern>/Servlet</url-pattern> </servlet-mapping> <session-config> <session-timeout> 30 </session-timeout> </session-config> </web-app>
Output
Run your code to get the following output:
Click on the link, you will find your data in PDF format displayed in the web browser directly.
If you want to see your page format then you just save this page and see it in PDF format.
Open this downloaded file, you will get the file in PDF format as shown below.
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.