title: Seeking a more efficient and reliable way to feed form data into a pdf doc via php
body: <p>For a few years starting back in 2005 or so I was looking for a way to use php to populate a pre-existing pdf document, as redrawing an existing pdf from scratch in php to have the exact same look as an existing pdf is one heluva task, esp when it comes to ever changing federal BK docs.</p>
  
  <p>After an eternal search, I found this code in 2008, which 'works', but is unreliable, in the sense that it may not 'find' the fdf file (which has to be dropped into the awaiting pdf) so the user has to try a few times, the fdf loads into the browser cache and the thing finally fires:</p>
  
  <pre><code>$tstamp = date('mdYHis');
  
  $test1 = mysql_real_escape_string($_POST['test1']);
  $test2 = mysql_real_escape_string($_POST['test2']);
  
  $fdf=dirname(__FILE__).'/doc_test/'.$tstamp.'.fdf';
  if (file_exists($fdf)) {unlink($fdf);} 
  
  require_once 'createFDF_doc_test.php';
  $pdf_file_doc_test=$link. '/forms/doc_test.pdf';
  
  // allow for up to 25 different files to be created, based on the minute
  $min=date('i') % 25;
  
  $fdf_file_dt=dirname(__FILE__).'/doc_test/'.$tstamp.'.fdf';
  
  if(isset($_POST['test1'])){
      $_POST['test1']=$_POST['test1'];
  
      // get the FDF file contents
      $fdf_doc_test=createFDF_doc_test($pdf_file_doc_test,$_POST);
  
      // Create a file for later use
      if($fp=fopen($fdf_file_dt,'w')){
          fwrite($fp,$fdf_doc_test,strlen($fdf_doc_test));
          $CREATED=TRUE;
      }else{
          echo 'Unable to create file: '.$fdf_file_dt.'&lt;br&gt;&lt;br&gt;';
          $CREATED=FALSE;
      }
      fclose($fp);
    } 
  </code></pre>
  
  <p>Here are the contents of createFDF_doc_test.php:</p>
  
  <pre><code>&lt;?php
  
  function createFDF_doc_test($file,$info){
      $data="%FDF-1.2\n%âãÏÓ\n1 0 obj\n&lt;&lt; \n/FDF &lt;&lt; /Fields [ ";
      foreach($info as $field =&gt; $val){
          if(is_array($val)){
              $data.='&lt;&lt;/T('.$field.')/V[';
              foreach($val as $opt)
                  $data.='('.trim($opt).')';
              $data.=']&gt;&gt;';
          }else{
              $data.='&lt;&lt;/T('.$field.')/V('.trim($val).')&gt;&gt;';
          }
     }
      $data.="] \n/F (".$file.") /ID [ &lt;".md5(time())."&gt;\n] &gt;&gt;".
          " \n&gt;&gt; \nendobj\ntrailer\n".
          "&lt;&lt;\n/Root 1 0 R \n\n&gt;&gt;\n%%EOF\n";
      return $data;
  }
  ?&gt;
  </code></pre>
  
  <p>What all this does is creates an fdf file and stores it in a folder on the server. The field names from a html form are matched with the pdf and the fdf acts as the conduit between the two.</p>
  
  <p>The content of the fdf, which is always successfully saved to the server, will look something like this:</p>
  
  <pre><code>%FDF-1.2 
  %âãÏÓ
  1 0 obj
  &lt;&lt; 
  /FDF &lt;&lt; /Fields [ &lt;&lt;/T(test1)/V(Test)&gt;&gt;&lt;&lt;/T(test2)/V(Complete)&gt;&gt;] 
  /F (http://example.com/forms/doc_test.pdf) /ID [ &lt;5d721ab873727ae4de3ebd21859bbc60&gt;
  ] &gt;&gt; 
  &gt;&gt; 
  endobj
  trailer
  &lt;&lt;
  /Root 1 0 R 
  
  &gt;&gt;
  %%EOF
  </code></pre>
  
  <p>The issue is the 'marriage' between the fdf and the pdf often doesn't fire because of some browser issue, or the timing the fdf is called, or something. Either way, the code is old, and I was hoping someone knew of a better, more efficient code that can quickly and buglessly move html form data, via php, into an awaiting pdf doc. I have been using the above for 8 years and I am tired of it.</p>