Populate existing PDF with .xfdf form data stored in database using PHP/MySQL

I have an HTML form that enters the values into a database. I use this PHP/MySQL code to retrieve the data and create the .xfdf form data, then the .xfdf form data is entered into the database:

function CreateXFDF($file,$info,$enc='UTF-8')
{
    $data='<?xml version="1.0" encoding="'.$enc.'"?>'."\n".
          '<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">'."\n".
            '<fields>'."\n";
    foreach($info as $field => $val){
          $data.='<field name="'.$field.'">'."\n";
        if(is_array($val)){
            foreach($val as $opt)
                $data.='<value>'.htmlentities($opt).'</value>'."\n";
        }else{
            $data.='<value>'.htmlentities($val).'</value>'."\n";
        }
        $data.='</field>'."\n";
    }
    $data.='</fields>'."\n".
          '<ids original="'.md5($file).'" modified="'.time().'" />'."\n".
          '<f href="'.$file.'" />'."\n".
          '</xfdf>'."\n";
    return $data;

   require_once('createxFDF.php');
   $id = $_GET['id'];
   $pdf_file='http://XXX.XXX.XXX.XXX/admin/application.pdf';
   include("login.php");
   login();
   $query = "select * from client where id=$id";
   $result = mysql_query($query);
   $row = mysql_fetch_array($result, MYSQL_ASSOC);

   $text_fields=array(
     $row['business_name'],
     $row['business_name2'],
     $row['business_address1'],
     $row['business_address2'],
     $row['legal_name'],
     $row['owner_first'],
     $row['owner_last'],
     $row['legal_phone'],
     $row['legal_fax'],
      );
        // get the xFDF file contents
        $xfdf=createxFDF($pdf_file,$text_fields);
        if (get_magic_quotes_gpc())
         {
          $xfdf = stripslashes($xfdf);
         }
       $xfdf=mysql_real_escape_string($xfdf);

      $query = "INSERT INTO applications SET apps='$xfdf', idnum='$id'";
      $result=mysql_query($query);
This works fine, using phpMyAdmin I can see the correct form data in the database.
Then when I try to retrieve the .xfdf form data from the database the existing PDF form (on the server) opens but the form data does not populate the fields in the form. Code:

   $idnum = $_GET['idnum'];
   include("login.php");
   login();
   $query = "SELECT * from applications where idnum=$idnum";
   $result = mysql_query($query);
   $row=mysql_fetch_array($result, MYSQL_ASSOC);
   $datafile = $row['apps'];
   header("Pragma: cache");
   header("Cache-Control: public");
   header("Content-Description: File Transfer");
   header("Content-type: application/vnd.adobe.xfdf");
   header("Content-Disposition: attachment; filename=application.xfdf");
   header("Expires: 0");
   echo $datafile;

I have based this code on code (which works fine) that takes the HTML field entries and directly populates the existing PDF form; but I need to have this performed from the .xfdf form data in the database not from the HTML form field $_POST values.
I have checked the field names in the PDF form are identical to the field names used to create the .xfdf form data file.

Client

-------------------------------------------

Here is an example of xfdf file which captured using the var_dump() function,

//contents of $datafile on line 16 captured by var_dump
array(2) {
  ["idnum"]=>
  string(2) "26"
  ["apps"]=>
  string(626) "<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field name="0">
<value>Pro Software Design</value>
</field>
<field name="1">
<value>108 W Jefferson St</value>
</field>
<field name="2">
<value>Hudson</value>
</field>
<field name="3">
<value>Brad</value>
</field>
<field name="4">
<value>Hudson</value>
</field>
<field name="5">
<value>5555555555</value>
</field>
<field name="6">
<value>5555555555</value>
</field>
</fields>
<ids original="ad8443f3c2cc8d79d9101d5c8c771bfa" modified="1283459509" />
<f href="http://XX.XXX.XXX.XXX/admin/application.pdf" />
</xfdf>
"
}
string(626) "<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field name="0">
<value>Pro Software Design</value>
</field>
<field name="1">
<value>108 W Jefferson St</value>
</field>
<field name="2">
<value>Hudson</value>
</field>
<field name="3">
<value>Brad</value>
</field>
<field name="4">
<value>Hudson</value>
</field>
<field name="5">
<value>5555555555</value>
</field>
<field name="6">
<value>5555555555</value>
</field>
</fields>
<ids original="ad8443f3c2cc8d79d9101d5c8c771bfa" modified="1283459509" />
<f href="http://XX.XX.XXX.XXX/admin/application.pdf" />
</xfdf>
"

//contents of database as viewed in phpMyAdmin
<?xml version="1.0" encoding="UTF-8"?>
<xfdf xmlns="http://ns.adobe.com/xfdf/" xml:space="preserve">
<fields>
<field name="0">
<value>Pro Software Design</value>
</field>
<field name="1">
<value>108 W Jefferson St</value>
</field>
<field name="2">
<value>Hudson</value>
</field>
<field name="3">
<value>Brad</value>
</field>
<field name="4">
<value>Hudson</value>
</field>
<field name="5">
<value>5555555555</value>
</field>
<field name="6">
<value>5555555555</value>
</field>
</fields>
<ids original="ad8443f3c2cc8d79d9101d5c8c771bfa" modified="1283459509" />
<f href="http://XX.XXX.XXX.XXX/admin/application.pdf" />
</xfdf>

Client

-------------------------------------------

Thanks for your message, our PDF Form Filler Cloud API is support the function of "Populate existing PDF with .xfdf form data stored in database using PHP/MySQL" now, please feel free contact us at http://support.verypdf.com, we will provide more information to you asap.

VeryPDF

VN:F [1.9.20_1166]
Rating: 7.3/10 (3 votes cast)
VN:F [1.9.20_1166]
Rating: -1 (from 1 vote)
Populate existing PDF with .xfdf form data stored in database using PHP/MySQL, 7.3 out of 10 based on 3 ratings

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *


Verify Code   If you cannot see the CheckCode image,please refresh the page again!