Generating PDF from a Servlet


You want to make a printer-friendly document using a format like Adobe PDF.


Use response.setContentType("application/pdf") and a third-party Java API that can generate PDF.


Portable Document Format (PDF) is a file format created by Adobe Systems Inc. PDF gives you full control over how your document looks, much more so than HTML, XML, or even Java’s printing routines (see Chapter 12). Adobe Acrobat is a set of programs for reading and writing PDF. Adobe itself does not publish a Java API for generating PDF from scratch, but it does publish the file format specification (Adobe Portable File Format Specification) and explicitly gives everyone permission to write software to generate and/or process PDF files. PDF is a good fit for processing by an object-oriented language like Java, as it’s an object-based text format. As a result, there are several PDF APIs available for Java, both free and commercial:

  • Sitraka/KL Group ( has a PDF API as well as charting and other widgets, and JProbe, a leading tuning tool.

  • StyleWriterEE (see

  • PDFLib GmbH ( produces PDFLib. PDFLib is mostly in C, with a Java wrapper; it also has bindings for several other popular languages.The source code is distributed, making it very cross-platform. It’s free for noncommercial use; for commercial use, a small licensing fee is required.

  • ReportLab ( is not for ...

Get Java Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.