ONJava.com -- The Independent Source for Enterprise Java
oreilly.comSafari Books Online.Conferences.

advertisement

AddThis Social Bookmark Button

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

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.