ColdFusion lets you extract data from a PDF form and write the output to an XML data file. To do this, you must save the form output as a PDF file. (The cfpdfform tag source must always be a PDF file.)
To write the output of a PDF file to an XML file, use the read action of the cfpdfform tag, as the following example shows:
<cfpdfform action="read" source="#empForm.txtsheet#.pdf" XMLdata="timesheets\#empForm.txtsheet#.xml"/>
To save disk space, you can delete the PDF file and maintain the XML data file. As long as you keep the blank PDF form used as the template, you can use the populate action to regenerate the PDF file. For more information on populating forms, see Populating a PDF form with XML data.
For an HTTP post submission, use the cfdump tag with the form name as the variable to display the data structure, as follows:
<cfdump var="#FORM.form1#">
Notice that the structure is not necessarily the same as the structure of the PDF file used as the template (before submission). For example, the structure of a form before submission might look like the following example:
struct |
||
---|---|---|
form1 |
struct |
|
|
txtDeptName |
[empty string] |
txtEMail |
[empty string] |
|
txtEmpID |
[empty string] |
|
txtFirstName |
[empty string] |
|
txtLastName |
[empty string] |
|
txtPhoneNum |
[empty string] |
After submission by using HTTP post, the resulting structure might look like the following example:
struct |
||||
---|---|---|---|---|
FORM1 |
struct |
|||
|
SUBFORM |
struct |
|
|
|
HEADER |
struct |
|
|
|
HTTPSUBMITBUTTON1 |
[empty string] |
||
|
TXTDEPTNAME |
Sales |
||
|
TXTFIRSTNAME |
Carolynn |
||
|
TXTLASTNAME |
Peterson |
||
|
TXTPHONENUM |
(617) 872-9178 |
||
TXTEMPID |
1 |
|
||
TXTEMAIL |
carolynp@company |
The difference in structure reflects internal rules applied by Acrobat for the HTTP post submission.
To extract the data from the HTTP post submission and update a database with the information, for example, map the database columns to the form fields, as the following code shows:
<cfquery name="updateEmpInfo" datasource="cfdocexamples"> UPDATE EMPLOYEES SET FIRSTNAME = "#FORM1.SUBFORM.HEADER.TXTFIRSTNAME#", LASTNAME = "#FORM1.SUBFORM.HEADER.TXTLASTNAME#", DEPARTMENT = "#FORM1.SUBFORM.HEADER.TXTDEPTNAME#", IM_ID = "#FORM1.SUBFORM.TXTEMAIL#", PHONE = "#FORM1.SUBFORM.HEADER.TXTPHONENUM#" WHERE EMP_ID = <cfqueryparam value="#FORM1.SUBFORM.TXTEMPID#"> </cfquery>
You can set a variable to create a shortcut to the field names, as the following code shows:
<cfset fields=#form1.subform.header#>
Use the cfoutput tag to display the form data:
<h3>Employee Information</h3> <cfoutput> <table> <tr> <td>Name:</td> <td>#fields.txtfirstname# #fields.txtlastname#</td> </tr> <tr> <td>Department:</td> <td>#fields.txtdeptname#</td> </tr> <tr> <td>E-Mail:</td> <td>#fields.txtemail#</td> <tr> <td>Phone:</td> <td>#fields.txtphonenum#</td> </tr> <table> </cfoutput>