Population PDF Forms with ColdFusion 8

May 14 2009

Population PDF Forms with ColdFusion 8

Posted by AJ Mercer at 3:48 PM CFML | ColdFusion

Now that Adobe owns ColdFusion, a lot of PDF functionality has been added to CF8. Amongst that is the ability to populate PDF Forms. This means you can pre-populate forms for clients / members - how many times have you had to fill out a form and think to yourself, 'they already know this stuff!!'

The client / member can then update any info if required. With the proper licencing, you can save the data in the PDF, or have the PDF submit the data back to the web server.

Recently I was given the job of reproducing Contract Notes for clients. The origion is produced by printing the data onto stationary; but as the client wont have the sationary, this is not going to work. No drama for me, I am a CF developer :-)

The first think I did was to open up the PDF in Adobe Acrobat and add fields to the document to create the PDF form. This document is not to be alter by the client so licencing was not an issue. In fact, I get ColdFusion to flatten the file after it is populated so the client can not modify that data and then print it.

In previous of CF, you would hvae to use CFPDF to read the document, then other CF tags to get the fields and poplaute and bluh bluh bluh - doesn't matter any more cos CF8 simplifies all that with a new tag - CFPDFFORM and it's buddy CFPDFFORMPARAM

<cfpdfform source="#pdfPath##pdfDoc#"
                 destination="#pdfPath##pdfTemp#"
                 action="populate"
                 overwrite="yes" >
    <cfpdfformparam name="Branch" value="#qryCnote.BranchName#" />
    <cfpdfformparam name="Advisor" value="#qryCnote.AdvisorName#" />
</cfpdfform>

Just add a CFPDFFORMPARAM for each form field and give it a value <hand in the air and shout> 'Clear!'

And to get rid of the form fields, just use the flatten attributes.

<cfpdf action="write"
           source="#pdfPath##pdfTemp#"
           destination="#pdfPath##pdfTemp#"
           flatten="yes"
           overwrite="yes" />

And finally, push it back to the browser

<cfheader NAME='Content-Disposition'
               VALUE='attachment;filename=#saveAsName#' />
<cfheader name="cache-control" value="" />
<cfheader name="pragma" value="" />

<cftry>
    <cfcontent type="application/pdf"
                    file="#pdfPath##pdfTemp#"
                    deletefile="yes"

                    reset="yes" />
    <cfcatch>
    <!--- prevent unnecessary log entries when user
           cancels download whilst it is in progress
    --->
    </cfcatch>
</cftry>