cfpdfform is one of the most powerful yet overlooked features of ColdFusion. Until now you have been working with "flat" PDF files. cfpdfform is useful when you have to generate forms that combine boilerplate text and dynamic text; it can also be used to collect data. There are two options in creating the initial PDF form: Adobe Acrobat and Adobe LiveCycle Designer. For the purpose of Learn ColdFusion in a Week, we will only be covering PDF's created using Adobe Acrobat.
Read a PDF Form
The source of the PDF can be a path to a PDF file or a variable that holds the byte array of the PDF. You can store the results of this read in a variable.
<cfpdfform action="read" source= "testForm.pdf" result="formData" /> <cfdump var="#formData#" label="formData" />
The above code will dump all the form names and the data they hold.
Populating a PDF Form
Once you know the name of the fields in your PDF, you can use cfpdfformparam inside the cfpdfform tag to populate the fields. The below code loads all the field names from testForm.pdf and loops through them, populating them with "XX".
<cfpdfform action="read" source= "testForm.pdf" result="formData"/> <cfpdfform action="populate" source="testForm.pdf" destination="testPDF.pdf" overwrite="yes"> <!--- Loop through all the Form Fields and populate them with xx ---> <cfloop collection="#formData#" item="item"> <cfpdfformparam name="#item#" value="XX" /> </cfloop> </cfpdfform> <cfpdf action="read" name="pdfData" source="testPDF.pdf" />
Submitting PDF Forms
In Adobe Acrobat, you can add a button to a form. The properties of the button have some action options. Actions are based on triggers to the button, like the "Mouse Up" action, for instance. One of the actions you can choose is Submit a form.
The Submit Form action has some options you can set as to what type of data will be submitted when the button is clicked; below, we have chosen HTML. This option will send all field names that contain data and other information the user has filled out to the chosen URL, just as an HTML form would. The PDF option will send the entire PDF along with the information the user has filled out.
When users click the PDF submit button, they will be informed that the document is trying to connect to a website and give them the option to trust the site. This is for security purposes; users should be informed prior that they will receive this warning and not to panic.
If the target page displays any output, it will be presented to the user in another PDF. When you have the data, you may access it in the form scope.