Summary
In a recent tutorial, Sun's Joshua Marinacci describes how XHTML and the iPDF library can be combined to generate high-quality, print-ready PDF documents from dynamic Web page content.
Advertisement
Generating PDFs in Java was made a lot easier by Bruno Lowagie's iText library. In a recent java.net article, Generating PDFs for Fun and Profit with Flying Saucer and iText, Josh Marinacci explains how to generate PDFs from dynamic Web content using iText in conjunction with the open-source XHTML renderer library, Flying Saucer:
Flying Saucer.. is an LGPLed Java library on java.net originally created by me and continually developed by the java.net community. Flying Saucer's primary purpose is to render spec-compliant XHTML and CSS 2.1 to the screen as a Swing component. Though it was originally intended for embedding markup into desktop applications.. Flying Saucer has been extended work with iText as well. This makes it very easy to render XHTML to PDFs, as well as to images and to the screen...
iText is a PDF generation library created by Bruno Lowagie and Paulo Soares, licensed under the LGPL and the Mozilla Public License... Using the iText API, you can produce paragraphs, headers, or any other PDF feature. Since the PDF imaging model is fairly similar to Java2D's model, Flying Saucer and iText can easily work together to produce PDFs...
In the article, Marinacci points out that while HTML content is almost always intended to be displayed inside a browser, PDF documents, by contrast, are aimed primarily for pretty printing. Thus visual appearance on the printed page is an important consideration for PDF generation. The Flying Saucer and iText solution Marinacci outlines takes this difference into account:
Paged media like printed documents or slideshows have certain features specific to pages. In particular, pages have specific sizes and margins. Text laid out for an 8 1/2 by 11 inch piece of paper will look very different than text for a paperback book, or a CD cover. In short, pages matter, and Flying Saucer gives you some control over pages using page-specific features in CSS.
Another feature Marinacci shows in the article is the ability to add images to a PDF document in a way that's friendly to the printed page. The examples in the article use CSS for styling the printed output.
What are your favorite methods of generating PDF from dynamic content?