Background:
In today's enterprise applications their is a big requirement for dynamic generation of PDF documents. These applications range from telecom companies generating phone bills, airlines producing e-tickets, banks generating customer statements for e-mail delivery to readers, book sellers selling books in pdf format.
What is PDF:
The Portable Document Format (PDF) is the file format created by Adobe Systems in 1993 for document exchange. PDF is used for representing two-dimensional documents in a device-independent and display resolution-independent fixed-layout document format. Each PDF file encapsulates a complete description of a 2-D document (and, with Acrobat 3-D, embedded 3-D documents) that includes the text, fonts, images, and 2-D vector graphics that compose the document.
Why PDF:
PDF is an open standard, unlike Microsoft Word, supported by all operating systems, ie; as long as you have an adobe reader you can read a pdf file on any system like Linux, Mac, Solaris, Windows. On virus front, it’s very hard that a PDF file will have some virus. It is is now being prepared for submission as an ISO standard.
Dynamic PDF Generation in Enterprise Application Development :
As mentioned above, in digital age everything is digitized so that end user can access it from anywhere, anytime. As a result new enterprise applications are generating lot of data in pdf formant which can be used by end user.
In Enterprise (Java or J2EE, JEE) application development generating pdf documents on the fly (dynamically) has become a trivial thing, courtesy of lot of third party tools, APIs available. The list of these tools/API's is endless. In this article, we will use the iText Java library to generate PDF documents by merging GIF files. I'll go through an example to show how this is done.
iText :
iText is an open-source Java library that provides API to generate PDF files on the fly. It also supports the generation of HTML, RTF, and XML documents, in addition to generating PDFs. It's available for free under a multiple license: MPL and LGPL.
The com.lowagie.text.Document is the main class for PDF document generation. This is the first class to be instantiated. Once the document is created, we would require a writer to write into it. The com.lowagie.text.pdf.PdfWriter is a PDF writer. Other classes which are often used:
- com.lowagie.text.Paragraph - represents an indented paragraph.
- com.lowagie.text.Chapter - represents a chapter in the PDF document. It is created using a
Paragraphas title and anintas chapter number. - com.lowagie.text.Font - contains all specifications of a font, such as family of font, size, style, and color. Various fonts are declared as static constants in this class.
- com.lowagie.text.List - represents a list, which, in turn, contains a number of
ListItems. - com.lowagie.text.Table - represents a table that contains cells, ordered in a matrix.
Example of converting multiple GIF files into PDF :
Pre-requisites :
Download iText and include itext-version.jar into your application or yr classpath.
Code Snippet :
The following code snippet demonstrates how to convert an array (collection) of gif files into a single pdf,
{code}
FileOutputStream fos = null;
try {
fos = new FileOutputStream (
new File(pDestinationFolder + File.separator
+ pTargetFileNamePrefix + ".pdf")
);
// Create a document which is the container for all the elements of a PDF document.
Document doc = new Document();
// Line -1
PdfWriter writer = PdfWriter.getInstance(doc, fos);
doc.open();
for (File aFile : gifFiles) {
Image image = Image.getInstance(aFile.getAbsolutePath());
// Line -2
image.scaleToFit(doc.getPageSize().getWidth(), doc.getPageSize().getHeight());
// Line -3
image.setAlignment(Image.ALIGN_CENTER);
doc.add(image);
doc.newPage();
}
doc.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
if (fos != null) {
try {
fos.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
{code}
Code Demystified :
- Line - 1 is important to create an instance of PdfWriter that associates a document object with the output stream. For our code snippet, we choose com.lowagie.text.pdf.PdfWriter. Other writers are HtmlWriter, RtfWriter, XmlWriter, and several others for obvious reasons.
- Line - 2 is very useful methods that will scale the current image to fit the width and height specified. Gif images can vary in sizes and this allows the method to scale down to the default size of the PDF page.
- Line-3 is to align the image so that it is located at the center of the page.
Other opensource libraries for generating pdf documents are :
- FOP
- Gnujpdf
- JFreeReport
- JPedal
- jPod
- PDF Box
- PDFjet
- PJX
5 Responses to Creating PDF Documents Dynamically in J2EE, Java Applications
On the surface, this seems like a good approach. I've found in practice, it breaks down pretty quickly when you need to start outputting special formatting (tables, wrapped text, etc). Building and configuring all of that with code turns into a maintenance nightmare. An alternative solution is to build use LifeCycle Designer to build PDFs that populate themselves by calling web services. I've found this solution costs less up-front *and* is easier to maintain.
Hi--Note that the Flying Saucer library also allows you to render to PDF, using XHTML (or any XML) and CSS 2.1. PDF output is also via iText, but for many purposes should be easier than using iText directly. Check it out when you get a chance!
http://xhtmlrenderer.dev.java.net
Regards
Patrick
Flying Saucer Team
Jboss Seam has nice integration with iText for enterprise applications.
http://docs.jboss.com/seam/2.0.0.GA/reference/en/html/itext.html
BTW, JBoss Seam 2.0 is out...
Hello,
I am working for a company which has a product to create a PDF documents dynamically. Our product will allow you to create PDF documents programmatically from scratch, merge existing PDF documents, add contents to existing PDF files, stamping PDFs, appending existing PDF documents, form filling, rotating and scaling PDFs, etc.
You can add tables and wrapped text to the PDF using the Table and TextArea page elements respectively. You can also take care of the overflow rows or text and you can add this overflows to a new page. You can do this until you have the overflows.
You will have to use our product DynamicPDF for Java when you are working with the java application. You can refer to our website at http://www.ceTe.com.
Thanks,
Ranga,
ceTe Software Support Team.
Now to add to this ..
Sun has released a new open-source project as part of SwingLabs: PDF Renderer, "a 100% Java PDF Renderer and Viewer." PDF Renderer can parse the Portable Document Format (PDF) from a file and display it, as an AWT image, in a panel, or using any Graphics2D implementation. It has been released under the LGPL license, the same license used by the rest of SwingLabs.
Something to say?