How do I serve up a PDF from a servlet?
Author: Deron Eriksson
Description: This tutorial describes how to serve up a PDF from a servlet.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 2.0 (Eclipse 3.3.0) || Tomcat 5.5.20

Page: < 1 2

(Continued from page 1)

If we hit the TestServlet, the browser asks us if we'd like to open or save the pdf-test.pdf file.

Hitting test servlet in web browser for PDF

If we open the file, we can see that the servletW did indeed serve up the PDF content via the response.

PDF viewed in Adobe Acrobat

This technique can be useful in a variety of ways. For example, PDF content can be generated dynamically and returned to a user via the response output stream without ever needing to create an actual file in the file system. In addition, having a servlet serve up PDF content can serve as a mechanism for restricting access to a PDF file in the file system or in a databaseW since a servlet can determine who should have access to a particular PDF file.

Page: < 1 2