This is a Flickr badge showing public photos from errorik. Make your own badge here.
25 years to life...
Recent Notable URLsXML
Panasonic SD5 - HiDef SDHC Camcorder
A self-purchase for my family and I. Upgrading from an old Sony Mini DV camcorder.Comment Icon
Interpol's got PhotoShop Skillz
Interpol has been able to reverse image effects unmasking a pedophile.Comment Icon
Amazon Launches DRM-Free MP3 Store
Cheaper than iTunes. Better quality than iTunes. DRM Free.Comment Icon
Raytheon brings the Pain Gun
"This machine has the ability to inflict limitless, unbearable pain."Comment Icon
How to Solve a Maze with Adobe Photoshop
This is the best hack of PhotoShop I have ever seen.Comment Icon
Urban Dictionary: Prewalking
Walking down the subway platform so that when you board the train, you'll be close to the exit or transfer point when the train reaches its destination.Comment Icon
How to Schedule MySQL Backups in Windows
Gotta love it when something you thought might be challenging is explained to be so easy.Comment Icon
Nintendo to release official MP3 Player for the Nintendo DS
The best handheld gaming system gets a media add-on. Nice.Comment Icon
Laptop sleeves like monster muppets
I don't care how much I like monsters and cartoons and such, I'd feel like the world's biggest weirdo if I actually carried this in public.Comment Icon
"Pee-wee's Playhouse" joining Cartoon Network's Adult Swim
Saturday mornings were so awesome as a kid.Comment Icon
[ more ]
Search the Notable URLs Archive:
Recent User Comments
Blog Archive
Search the Blog Archive:
Friends and others
Michael; Nate & Val; BJay; Jessica; Joel; Steven Ng; AJ; Kirker; Derek;
Get Firefox

Creative Commons License
All content on this site (including text, photographs, artwork, and any other original works), unless otherwise noted, is licensed under a Creative Commons License.

Valid XHTML 1.0!

ColdFusion MX and iText for PDF Generation

One of the greatest powers of Adobe's ColdFusion MX server is the ability to utilize Java libraries for additional functionality.
I recently had a friend ask me if I could generate documents based on data collected from a web form. Sure thing, no problem. Then he showed me his template that he wanted the data inserted into. It wad a highly stylized Microsoft Word document with embedded images. I told him that it wouldn't be as simple as I had originally planned (I was thinking of outputting RTF files at first), and I asked if Adobe PDF files would work, which he answered "yes" to.
I quickly realized that while I have been enjoying working within my employer's ColdFusion MX 7 server environment with it's built-in PDF generation thanks to the CFDOCUMENT tag, my own server is only running version MX 6 and I am lacking the capabilities... Until I take advantage of a 3rd party component at least.
I had heard of stuff like CutePDF and I wanted to get more info. So I turned to mankind's best friend, Google.
I ran across a buddy, and former co-worker, of mine, Aaron Johnson's blog where he wrote a how-to on generating PDFs from scratch using ColdFusion and the free Java library iText. This wasn't exactly what I wanted as I was looking to setup pre-defined regions in a PDF template, then generate copies populated with dynamic data.
This was something I knew that something out there could do, I just didn't know how.
Looking into iText some more I found that it had what is called a PDF Stamper that could populate PDF form fields with data. "PERFECT", I thought. But my searches to find a ColdFusion example of using the Stamper showed me that there wasn't a whole lot of people doing this.
I did however find a pure Java example of using the Stamper how I wanted, and using some common sense, Aaron's ColdFusion code as a base, and about an hour of trial and error I was able to get a working ColdFusion/iText app going.
I am here today to spread the knowledge.
First things first. Install iText by downloading the Java JAR file from their web site and placing it in your ColdFusion /lib/ folder. Now Stop/Start the ColdFusion server service and you are done with step one. iText is now installed.
Next you need a PDF with form fields to work with. You can create your own or use this one here: SimpleRegistrationForm.pdf (taken from iText example page).
And finally here is the CFML to load the PDF template, set the values for the PDF form fields, and save the results to a new PDF:
//define path and file name of the PDF template with form
//define the name of my output file

//create the output file
//load the template PDF with the iText PDF reader
reader = createObject("java","com.lowagie.text.pdf.PdfReader").init(pdfFile);
//load the template into the iText PDF stamper and specify the output file
pdfStamper = createObject("java","com.lowagie.text.pdf.PdfStamper").init(reader, fileIO);

//create a form object to reference
pdfForm = pdfStamper.getAcroFields();

//set the form fields
pdfForm.setField("name", "John Doe");
pdfForm.setField("email", "john@gmail.com");
pdfForm.setField("address", "123 Sesame St.");
pdfForm.setField("postal_code", "90210");

//optional, this will "flatten" the form in the output file to convert all the editable form fields to plain text, or you can remove this to leave them as editable form fields

//you are done now.
You can download the output file here: SimpleRegistrationForm_output.pdf
As you can see I am using the setField method to set the values of the form fields. And I have to name each field by how they were named in the original PDF. If by chance I didn't know all the names I could just use CFDUMP and get the form field names all returned to me:
<cfdump var="#pdfForm.getFields()#">
A great thing about this is that you can have non-developers handle creating the PDF templates for you. Using the Adobe Acrobat software it is very easy to define form fields within your document.
Even with CFMX 7's added built-in PDF support, I think iText is a welcome addition giving you even more functionality. And it's priced right.

User Comments

Friday, July 7th 2006
This is very nice and exactly what I was looking for!! Thanks.
Thursday, December 7th 2006
That's very innovative blog of urs! I am really IMPRESSED!!

I have a problem with adding images to PDF doc using IText.
say i have a code::
image = CreateObject("java", "com.lowagie.text.Image");

But it throws an Error:
The system has attempted to use an undefined value, which usually indicates a programming error, either in your code or some system code.
Null Pointers are another name for undefined values.

Can u help me out?
Thursday, December 7th 2006
Hmmm... I haven't had to do that yet... so I don't know off hand.

I assume that your app requirements call for the image to be dynamic (like from an upload)... in my app where I use iText my PDF template that I am "stamping" has image in it, but they are always the same (I don't use iText to add them in).

Best of luck, and if I find anything out I will let you know.
Thursday, May 17th 2007
Thank's for the help man, there was no human speech / text about this subject anywhere. Nothing but bland docs & links. Your a champion of sorts.
Thursday, May 17th 2007
To anyone attempting the above:

It may be worth mentioning that if you do not flatten the file, the populated fields will not show. I'm not sure if this is because I'm using acrobat 8, or I'm doing something wrong, but i found that when using the above method with cf mx7 , the same code, and firefox/acrobat 8 that i did indeed have to flatten the file. iText 2.0.2.
Monday, June 11th 2007
Cara você é foda, perfeito, procurei em tudo quanto foi lugar, fiquei horas pesquisando no google e não tinha achado nada até encontrar seu site com este exemplo. PERFEITO!!!!

Tuesday, June 12th 2007
Succint and excellent. Thank you.

p.s. The mathematic captcha thing did not work on my browser of choice (IE7) so I had to use firefox to add this comment [shudders] :p
Tuesday, June 12th 2007
Thanks for the IE info. I will check it out.

Add your own comment

Comment system temporarilty off-line.