Processing HTML Form data to XFDF

The first step in the process is to collect the data submitted to the script that does all this work. After all filtering of input has been done, our clean, usable data has been stored in the associative array $data. For illustrative purposes, the data we will use here looks like the following:

$data = array (
    'first_name' => 'John',
    'last_name'  => 'Doe',
    'email'      => 'jdoe@example.com',
    'dob'        => '1980-04-14',
    'notes'      => 'It\'s a plain text field that contains stuff.'
);

We also have some additional pieces we will need in order to generate the XFDF. The data will need to have a reference to the file it belongs to, so we will use a variable holding the URL of the PDF form file to fill the data into. This can be either a public or private URL, but if it requires authentication, results differ. I suggest starting testing on a publicly accessible file.

$pdf_file_url = 'http://example.com/from.pdf';

We are now ready to take this cleaned data and create the XFDF that will be used. Simply include the function definition and call it to produce a string:

include 'createXFDF.php';
$xfdf = createXFDF( $pdf_file_url, $data );

That really is all there is to the generation of the XFDF data. Why make a separate sub-article for this? Well, I just didn't want to copy/paste for each of the other sub-articles! When everything is running properly, the above should generate the following content in the $xfdf variable (below if formatted for readability):

<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
  <fields>
    <field name="first_name">
      <value>John</value>
    </field>
    <field name="last_name">
      <value>Doe</value>
    </field>
    <field name="email">
      <value>jdoe@example.com</value>
    </field>
    <field name="dob">
      <value>1980-04-14</value>
    </field>
    <field name="notes">
      <value>It's a plain text field that contains stuff.</value>
    </field>
  </fields>
  <ids original="d8b5fb00b50e5bf5caca4aa92ea9253b" modified="1363280525" />
  <f href="http://example.com/from.pdf" />
</xfdf>