Introduction
This article explains how to write data in PDF format using a servlet in Java. The NetBeans IDE is used for creating this application.
Note:
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:
- HTML file
- servlet file
- 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.
Open the NetBeans ide.
Fig-1
Step 2
Choose "Java web" -> "Web application" as in the following.
Step 3
Type your project names as "PDFApp" as in the following.
Step 4
Choose your version and server wizard as in the following.
Step 5
Now delete your default "index.html" file and create a new "index.html" file and provide the following for it:
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="PDFServlet">Click Here To See your content</a>
</body>
</html>
Step 6
Create a servlet named "PDFServlet" and and provide the following for it:
PDFServlet.java
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.darwinsys.spdf.*;
public class PDFServlet extends HttpServlet {
@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 Formated"));
pag.add(new Text(pdf,"This application is created by Sandeep"));
pag.add(new Text(pdf,"Thanks to visit this application"));
pdf.add(pag);
pdf.writePDF();
out.close();
}
}
Note:
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.
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>PDFServlet</servlet-name>
<servlet-class>PDFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PDFServlet</servlet-name>
<url-pattern>/PDFServlet</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
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.
Step 10
If you want to se your page format then you just save this page and you see it in PDF format.
Step 11
Open this downloaded file, you get the file in PDF format as shown below.