ColdFusion MX and iText for PDF Generation
One of the greatest powers of Adobe's ColdFusion MX server
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
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("address", "123 Sesame St.");
//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:
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
Boise Area Blogger's Dinner at the Flying Pie Pizzeria
Tonight I attended the 1st ever Boise Area "Blogger's Dinner". A group of maybe 15-20 so called "bloggers" met up at the Flying Pie Pizzeria
were pizza was being served left and right since one of the bloggers in attendance was none other than Flora
, the near 200 lbs ball of foil that calls the Flying Pie home.
I didn't get to talk to everyone there face-to-face, nor did I take good notes on everyone, but I hope to be able to provide links out to all that I can gather info on. Some people contribute to over 10 blogs, I don't intend to research everyone that far, but a link a piece if I can.
If you happened to be at the dinner and I didn't list you above, please leave a comment or send me an e-mail.
One person there said that he ran/contributed to a blog that solely posts entries about stories of people that used firearms for self-defense, I am really interested in reading/linking to your blog, so if you happen to find this please post the link in the comments.
Mario Has Made Me A Kid Again
Today Nintendo releases New Super Mario Bros
(NSMB) for the Nintendo DS
and I couldn't be more excited. NSMB is a return to the classic side-scrolling Mario found on the NES and Super NES. Mario, Princess, Luigi, Goombas, and Power Ups galore. I can't wait. I am so worked up for the release that last week I went into a local game shop and placed my $5 on the counter and asked to pre-order the game.
It hasn't been since I was a young child that I have pre-ordered a video game without there being some type of additional reward for the action (like a bonus game disc with Mario Kart: Double Dash
, or a Gold Edition of The Legend of Zelda
cartridge, or maybe 10% off the price like Valve does on their games like Half-Life 2
It was somewhat thrilling to see my name get written down (4th on the list), and have the young girl running the register tell me that I will get a phone call when the game arrives. When she told me that they would hold the game for some ridiculous time period of like 10 days, I had to restrain myself from blurting out something stupid like "No need, I will be here within 10 minutes... har har har".
The courteously call of the game arriving is almost worth the drive of going to the game store twice. I can assure myself that I will get the game the day the store receives it (most likely tomorrow), and I don't have to make multiple calls to the store to check and see if they "have the game in yet". And on top of that, have some young clerk see me when I come in and think "man, this is the old guy that called us like 10 times today?" Yes, I do consider myself the "old guy" at times, even when I am only 27, I guess being a Nintendo fanboy at my age I am asking for it.
I have to admit that I will pre-order items on Amazon and other on-line shops. But that almost doesn't count since all on-line orders have some delay attached to them. You place your order, and it ships as soon as it is ready, whether the delay comes from a slow warehouse, items being out of stock or unreleased, it is almost irrelevant. I will get an e-mail from Amazon when the next Seinfeld DVD
will be released, it would be stupid for me to wait until after the release before going to the web site and placing my order when the experience of ordering the DVD months before the release is exactly the same, no more difficult at all. Just send me my stuff when it's out. It's almost like surprise when you get an e-mail saying that your order has shipped.
Back to the video game... yea... um... I can't wait. And I am such a geek I am thinking about ordering this "I Scored With The Princess
" T-Shirt from the UK.
Nintendo opened up the full web site for NSMB, check out out at http://mario.nintendo.com/