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.

iText API :

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 Paragraph as title and an int as 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
Del.icio.us Digg! My StumbleUpon Page