Dynamically Creating PDFs in a Web Application
Pages: 1, 2
Content-type
In servlets, HttpServletResponse has a content type that
indicates the type of content that the response contains. For PDF files, the
content type is application/pdf. If the servlet does not set a
content type, the web browser may have a difficult time determining how to
handle the file.
PDFServlet sets the content type with the following line:
resp.setContentType("application/pdf");
Content-disposition
The Content-disposition header provides information that helps
a web browser identify the content of the HTTP response. When a web browser
reads this header, it can determine:
- That the HTTP response contains a file.
- The name of the file contained in the response.
- Whether the file should be displayed inside of the browser's main window or viewed by an external application.
RFC 2183 provides a full explanation of the Content-disposition header.
By setting the Content-disposition header
appropriately, the servlet can instruct the browser to display the
file "inline," or to treat it like an attachment.
Example 1. Displaying a file inline
Content-disposition: inline; filename=foobar.pdf
Example 2. Attaching a file to the response
Content-disposition: attachment; filename=foobar.pdf
The following pseudo-code demonstrates how to set the header:
public void doGet(HttpServletRequest req, HttpServletResponse resp)
{
// ...
resp.setHeader(
"Content-disposition",
"inline; filename=foobar.pdf" );
// ...
}
Cache-Control Headers
Depending upon the nature of your application, you may or may not want web browsers to cache the PDF files that you are generating. There are a variety of HTTP headers that a server-side web application can use to control caching of content. Some examples are:
-
Cache-Control: no-cache -
Cache-Control: no-store -
Cache-Control: must-revalidate -
Cache-Control: max-age=30 -
Pragma: no-cache -
Expires: 0
A full explanation of Cache-Control headers is found
in the HTTP 1.1 specification.
The PDFServlet sets Cache-Control to
max-age=30. This header tells the web browser to cache the file
for a maximum of 30 seconds.
Content-length
The Content-length header must be set to the number of bytes in
the PDF file. If the Content-length header is not set correctly,
the web browser may not be able to display the file. Example code might
be:
ByteArrayOutputStream baos = getByteArrayOutputStream();
resp.setContentLength(baos.size());
Sending the PDF Document to a Web Browser
PDFServlet sends the PDF document to the client by writing
bytes to the servlet's output stream. It obtains the output stream by calling
getOutputStream() on the HttpServletResponse object.
getOutputStream returns an object of type
javax.servlet.ServletOutputStream.
ServletOutputStream sos;
sos = resp.getOutputStream();
baos.writeTo(sos);
sos.flush();
After writing all data to the stream, call the flush()
method to send all bytes to the client.
Packaging and Deployment
To run the PDFServlet in Tomcat, you'll need to package the application in a
WAR file. The iText JAR file (itext-0.99.jar) must be placed in
the WAR file's lib directory. If you forget to include the
iText JAR file, the servlet will fail with a
java.lang.NoClassDefFoundError.
Running the Application
After the WAR file has been deployed, you are ready to test the servlet. Jakarta Tomcat listens for requests on port 8080.
Point your web browser to http://hostname:8080/pdfservlet/createpdf.
When you visit the URL, the servlet executes and sends a PDF document back to your browser.
Beyond iText
iText provides a great low-level API for producing PDF documents. However, it may not be the best tool for every application.
At my day job, we used iText in combination with Microsoft Word and Adobe Acrobat. First, our team designed a shipment form using Microsoft Word. Next, we converted the Word document to PDF using Adobe Acrobat. Then, using iText's template capability, we loaded the PDF file into our application. From there, it was quite easy to fill in data values on the form and output the final PDF document.
For report-oriented web applications, tools like JasperReports provide a higher level of abstraction than iText.
Conclusion
When your Java application needs to dynamically create PDF documents, the iText class library is a great solution. You can experiment with iText's capabilities by enhancing and extending the code in this article. In a short time, you'll be able to impress your co-workers and customers with sophisticated PDF documents.
Additional Resources
- http://www.lowagie.com/iText/
- www.jpedal.org
- www.pdfbox.org
- xml.apache.org/fop
- HTTP 1.1 protocol specification
- RFC 2183
- dmoz.org/Computers/Data_Formats/Document/Publishing/PDF
- www.planetpdf.com
- www.pdfzone.com
If you are exploring Microsoft's .NET platform, be sure to check out iTextdotNet and iTextSharp. Both projects are derived from the Java-based iText library. iTextSharp is written in Microsoft's C# language.
Sean C. Sullivan has been developing Internet applications with Java since 1996. His recent work includes B2B web applications, various open source projects, and the development of an Internet e-commerce payment system at Intel.
Return to ONJava.com.
-
convert html to pdf
2009-01-16 04:33:50 zeet [View]
-
Memory Issue!!
2008-06-19 19:31:28 shinnjava [View]
-
Dynamic pdf to image display on webpage
2008-03-12 08:01:31 Jyro [View]
-
Dynamic pdf to image display on webpage
2008-04-21 22:40:22 touchpdf [View]
-
Please help me
2007-10-09 01:46:52 meen [View]
-
Please help me
2007-10-09 03:36:33 rita11 [View]
-
Please help me
2007-10-09 01:46:52 meen [View]
-
generating big tables.
2006-10-29 22:21:48 abetme [View]
-
Erro
2006-08-11 06:42:53 ricardinho_rio [View]
-
How I Convert Html file to PDF file
2006-06-08 22:16:33 singla [View]
-
How I Convert Html file to PDF file
2006-09-17 22:47:17 SwapnilDeshmukh [View]
-
How I Convert Html file to PDF file
2007-01-24 23:03:32 yuvraj.kokitkar [View]
-
How I Convert Html file to PDF file
2006-08-02 07:30:56 maddirala [View]
-
Adding an image to an iText pdf
2006-02-23 08:04:40 rossington [View]
-
Adding an image to an iText pdf
2006-10-13 05:38:08 rubenpaxi [View]
-
Adding an image to an iText pdf
2009-12-19 00:30:57 arshika [View]
-
Convert .xsl to pdf
2006-02-16 07:02:49 Thirunamalli [View]
-
help please
2006-01-17 12:22:08 link1984 [View]
-
some error
2005-12-27 05:15:24 firdose [View]
-
Using iText
2005-09-20 02:11:25 Menon [View]
-
Using iText
2005-12-14 09:54:34 Praths [View]
-
API for creating press optimized PDF
2005-09-07 04:38:44 john1234 [View]
- Trackback from http://cephas.net/blog/2005/07/08/links_772005.html
Links: 7-7-2005
2005-07-08 11:54:45 [View]
-
PDF Overlay Issue
2005-03-03 15:04:27 summitsoft [View]
-
Dynamic PDF generation
2005-02-25 22:31:15 nageshever [View]
-
Dynamic PDF generation
2005-06-06 13:16:21 timle [View]
-
PDF file size
2004-11-22 23:52:55 Jlatorre [View]
- Trackback from http://jira.amphora-research.com/browse/PS-141
[PS-141] Set filename of PDF we render out
2004-11-07 15:17:51 [View]
- Trackback from http://jira.amphora-research.com/browse/PS-141
[PS-141] Set filename of PDF we render out
2004-11-06 02:00:24 [View]
-
Pricing of software
2004-09-09 19:32:52 i_fadhilah [View]
-
Pricing of software
2005-01-17 04:32:13 dacan [View]
-
saving created PDFs
2004-08-27 06:59:19 atp [View]
-
How to use iText template to fill data values on a Form?
2004-08-05 15:38:33 sunni [View]
-
How to use iText template to fill data values on a Form?
2005-05-05 06:20:54 ShubhadaAnand [View]
-
How to use iText template to fill data values on a Form?
2005-03-18 01:11:43 baby2046 [View]
-
How to use iText template to fill data values on a Form?
2006-10-12 16:58:57 dynamicpdf [View]
-
Dynamically putting Report data in PDF
2004-07-20 14:02:33 WPTony [View]
- Trackback from http://jira.amphora-research.com/browse/PS-141
[PS-141] Set filename of PDF we render out
2004-07-16 05:49:51 [View]
- Trackback from http://jira.amphora-research.com/browse/PS-141
[PS-141] Set filename of PDF we render out
2004-07-05 12:05:41 [View]
- Trackback from http://jira.amphora-research.com/browse/PS-141
[PS-141] Set filename of PDF we render out
2004-06-24 00:15:10 [View]
-
National Charters
2004-06-17 02:24:40 vasily [View]
-
Open Source PDF Libraries
2004-06-12 17:58:15 fj_rodriguez [View]
- Trackback from http://war625.egloos.com/76000
PDF »ý¼º¸ðµâ Âü°í ÀÚ·á
2004-06-06 01:30:48 [View]
-
Read pdf Header.
2004-05-24 00:24:54 Aurelien [View]
-
Complete example
2004-04-08 11:02:28 garygrub [View]
-
getParameterMap problem
2004-02-25 09:54:19 cameronp [View]
-
getParameterMap problem, me too
2004-04-27 06:50:37 illuminers [View]
-
Font in a Table
2004-02-03 00:12:50 javaduda [View]
-
Font in a Table
2004-04-21 11:38:15 Retric [View]
-
Memory Issues?
2004-02-02 14:30:33 jbradfor [View]
-
Converting PS to PDF
2004-01-12 22:27:06 sumit_only [View]
-
Printing PDF: Can I dynamically "uncheck" 'Shrink oversized pages to paper size' option for printing?
2003-12-29 12:24:21 anonymous2 [View]
-
Printing PDF: Can I dynamically "uncheck" 'Shrink oversized pages to paper size' option for printing?
2004-01-07 19:19:08 anonymous2 [View]
-
Problem when multiple PDF are to be generated
2003-12-17 21:06:27 anonymous2 [View]
-
Problem when multiple PDF are to be generated
2004-04-21 11:24:52 Retric [View]
-
Problem when multiple PDF are to be generated
2003-12-28 09:00:11 anonymous2 [View]
-
HTML to PDF conversion
2003-12-02 19:33:17 anonymous2 [View]
-
HTML to PDF conversion
2006-06-08 09:29:26 singla [View]
-
HTML to PDF
2003-12-02 19:30:03 anonymous2 [View]
-
Generate a .PDF and .xls on a fly from a HTML page
2003-11-13 16:52:00 anonymous2 [View]
-
Generate a .PDF and .xls on a fly from a HTML page
2003-11-19 09:07:49 anonymous2 [View]
-
Generate a .PDF and .xls on a fly from a HTML page
2005-01-22 21:33:03 new_iTextUser [View]
-
Generate a .PDF and .xls on a fly from a HTML page
2003-11-18 21:21:38 anonymous2 [View]
-
Generate a .PDF and .xls on a fly from a HTML page
2005-09-24 01:30:26 Nash-Java [View]
-
iText in combination with Microsoft Word and Adobe Acrobat
2003-11-05 19:38:48 anonymous2 [View]
-
iText in combination with Microsoft Word and Adobe Acrobat
2003-11-15 10:46:57 anonymous2 [View]
-
iText in combination with Microsoft Word and Adobe Acrobat
2003-11-08 07:49:02 anonymous2 [View]
-
iText in combination with Microsoft Word and Adobe Acrobat
2003-11-17 14:56:33 anonymous2 [View]
-
Acrobat Reader Compatibility ?
2003-11-05 19:36:19 anonymous2 [View]
-
problem
2003-10-20 05:11:27 anonymous2 [View]
-
pdf does not load with IE 6.0.2800.1106
2003-10-15 09:44:38 anonymous2 [View]
-
pdf does not load with IE 6.0.2800.1106
2004-03-02 12:33:51 joeseminara [View]
-
pdf does not load with IE 6.0.2800.1106
2004-09-14 11:56:27 rshimpi [View]
-
pdf does not load with IE 6.0.2800.1106
2003-11-21 11:32:17 anonymous2 [View]
-
pdf does not load with IE 6.0.2800.1106
2003-10-18 14:38:24 anonymous2 [View]
-
pdf does not load with IE 6.0.2800.1106
2004-10-20 09:34:04 subashk [View]
-
Big table generation problem
2003-10-11 10:18:19 anonymous2 [View]
-
Big table generation problem
2004-03-24 01:12:45 jms_knaack [View]
-
Big table generation problem
2006-10-29 22:16:50 abetme [View]
-
Big table generation problem
2003-10-18 14:39:17 anonymous2 [View]
-
I'm a layman - private publisher
2003-10-03 13:46:04 anonymous2 [View]
-
I'm a layman - private publisher
2004-04-21 11:35:22 Retric [View]
-
Answer required
2003-09-30 03:23:08 anonymous2 [View]
-
browser displaying pdf probs
2003-08-18 01:34:17 anonymous2 [View]
-
browser displaying pdf probs
2003-08-18 15:48:41 anonymous2 [View]
-
browser displaying pdf probs
2003-08-18 01:34:05 anonymous2 [View]
-
Exception in thread "main"
2003-08-15 02:36:27 anonymous2 [View]
-
Exception in thread "main"
2003-10-06 06:08:06 anonymous2 [View]
-
exception error upon compilation
2003-08-09 16:00:59 anonymous2 [View]
-
exception error upon compilation
2003-10-31 11:21:24 anonymous2 [View]
-
exception error upon compilation
2003-08-18 15:47:49 anonymous2 [View]
-
exception error upon compilation
2003-08-20 17:52:38 anonymous2 [View]
-
Use for business card template?
2003-07-24 09:11:33 anonymous2 [View]
-
Can I use iText to create PDFs on the client?
2003-07-10 12:00:54 anonymous2 [View]
-
Can I use iText to create PDFs on the client?
2003-07-16 15:30:58 anonymous2 [View]
-
pdf example (1.2.2)
2003-06-30 23:01:20 anonymous2 [View]
-
pdf example (1.2.2)
2003-07-01 15:07:58 anonymous2 [View]
-
itext-questions mailing list
2003-06-30 10:50:26 anonymous2 [View]
-
iText version 1.0
2003-06-30 10:48:38 anonymous2 [View]
-
FYI about caching PDFs
2003-06-22 19:12:32 anonymous2 [View]
-
Content-disposition inline
2003-06-20 00:30:07 mvg [View]
-
Content-disposition inline
2003-06-28 10:04:10 sullis [View]
-
Content-disposition inline
2003-09-23 11:53:28 anonymous2 [View]
-
FOP
2003-06-19 09:45:53 gadders [View]
-
FOP
2003-06-22 19:20:46 anonymous2 [View]
-
FOP
2003-06-20 02:29:33 anonymous2 [View]
-
FOP
2004-02-04 06:32:50 kercadio [View]
-
coordinate based tools
2003-06-19 20:58:34 anonymous2 [View]
-
coordinate based tools
2003-06-20 02:19:38 anonymous2 [View]
-
iText template
2003-06-19 02:08:03 anonymous2 [View]
-
iText template
2003-06-19 02:07:57 anonymous2 [View]