The Largest Repository of ColdFusion Knowledge in The World for More Than 12 Years

ColdFusion on Ulitzer

Subscribe to ColdFusion on Ulitzer: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get ColdFusion on Ulitzer: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn

CFDJ Authors: AppDynamics Blog, Michael Kopp, Tad Anderson, Bob Gourley, Jayaram Krishnaswamy

Related Topics: ColdFusion on Ulitzer

CFDJ: Article

Handling PDF Forms with ColdFusion

A cost-efficient alternative to Adobe's enterprise PDF server

Over the past few years government agencies and businesses have converted many of their paper forms to static PDF documents, making them available on Web sites as downloadable files for users to print locally, fill out, and submit by snail mail. All Internal Revenue Service forms are now available this way.

However, it doesn't take long to realize that if the data on mailed-in PDF documents needs to be input into a database, or if the filled-in form needs to be kept as an electronic image, it's more efficient to have the users do the data entry in the first place.

One option is to deploy Adobe's enterprise PDF server. Although powerful and elegant, this solution is very costly not only because of the hardware, operating system, and application software needed for purchasing, but also because it represents yet-another complex server to maintain and patch. A simpler and inexpensive option is to deploy a ColdFusion-accessible utility that has methods for reading and writing PDF forms. ActivePDF's 'Toolkit' is one such utility and will handle most form-entry and imaging needs.

Setting Up
ActivePDF Toolkit requires a Windows environment; the component, APToolkitNET.dll, is installed like any Windows software by invoking an executable. The component has to be registered with the CF/Web server so that when instantiated by a CF template, the operating system knows where to find it. Once installed and registered, the workflow for each PDF Web form follows this general pattern: (1) create a PDF Web form with one of several PDF Writer packages available (such as Adobe Acrobat Professional or the newly released and less expensive PDF Nitro), (2) code the ColdFusion template(s) for handling the form after a user has submitted it. This workflow is illustrated in the example below, where the user-submitted PDF form is e-mailed to a business unit and the values inserted into a database.

Create the PDF Form
Forms are usually created by a word processor or graphics program and subsequently opened in one of the PDF writers mentioned above. When open, you add form fields and a submit button via the writer's wizard. By default, the wizard will name the form fields serially (Text1, Text2...), but if the form is submitted for database insertion, the form fields should be meaningfully named. When configuring the submit button in the writer's wizard, the URL of the file to which it will be submitted should be specified; in this example, the form is submitted to 'MyBusinessForm.cfm.' After adding form fields and a submit button, save the new PDF document to a directory on your Web server. The URL of the PDF document is then advertised to potential users, which in this example might be 'http://www.serverName/aDirectory/MyBusinessForm.pdf.' Figure 1 shows a screen shot of the PDF as it appears in the browser and - without the 'Submit' and 'Reset' buttons - as an e-mail.

Handle the Form: ColdFusion coding
The form's action file in the example is 'MyBusinessForm.cfm' shown in Listing 1 at the end of this article. There are two sections.

The first section loops through the form fields, assigning its values to a structure. For testing purposes, those values are also printed to the browser screen. Next, the form's values are inserted into a database. Figure 2 shows a screen shot of running the example. You'll notice that PDF forms function much like <html..> or <cfform...> forms.

The second section is for PDF image creation. The code creates an instance of the APToolkit object and, using the methods supported by the COM component, opens and reads the original PDF ('MyBusinessForm.pdf') into memory, and then creates a new PDF image file named with a uniqueID. The next section loops through the submitted form fields and copies them into the appropriate fields in memory, removing the submit button. The next section copies the in-memory data over to the new PDF image file. After closing the image file, it's attached to an e-mail sent to the business unit and, optionally, to the user. The image file, now no longer needed, is deleted.

It's important to destroy the APToolkit object, lest you fill up the server's memory and cause a crash. The code to do it in CF v4.5 is <cfset PDF = "">; the same code will probably work in CFMX, although it might be preferable to use <cfset ReleaseComObject(PDF)>.

Enhancing the Functionality
The APToolkit object has many more methods than those demonstrated here, giving you very granular control over the formatting of the PDF image file; they are well documented in the product's literature. (The documentation also explains the parameters seen in Listing 1.)

To eliminate the possibility of the business unit's Web editors messing around in the guts of the handler, MyBusinessForm.cfm's actions should be encapsulated in a custom tag or a CFC. Another enhancement, necessary in a world infested with malware, should be filtering out form values that might perform cross-site scripting and/or sql injection attacks.

More Stories By Tim Burton

Tim Burton is the eGovernment Applications Architect for a large state agency in Oregon and has been writing CFML since 1998. This is his third career; he previously practiced medicine and made art (metal sculpture).

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

Most Recent Comments
CFDJ News Desk 08/17/05 09:06:12 AM EDT

Handling PDF Forms with ColdFusion. Over the past few years government agencies and businesses have converted many of their paper forms to static PDF documents, making them available on Web sites as downloadable files for users to print locally, fill out, and submit by snail mail. All Internal Revenue Service forms are now available this way.