title: fpdf, fpdi, fpdm & fpdf_merge with multi page pdf - php conflicts
body: <p>Hoping for some help. I'm trying to use the php below to do 4 things. The 1st long chunk adds a signature image to my sig.pdf template. The 2nd chunk fills form fields in my template3.pdf template.The 3rd chunk should merge both files. And the 4th chunk emails the merged pdf. </p>
  
  <p>1 and 4 by themselves work, and 2 &amp; 4 by themselves work. </p>
  
  <p>With 1, 2, and 4 together the two separate pdfs are created, but the email isn't sent. </p>
  
  <p>3 never works. I get the following error: </p>
  
  <pre><code>Fatal error: Uncaught exception 'Exception' with message 'object [1] non trouve' in /home/content/.../html/pdf2/fpdf_merge.php:74 Stack trace: #0 /home/content/.../html/pdf2/fpdf_merge.php(214): FPDF_Merge-&gt;error('object [1] non ...') #1 /home/content/.../html/pdf2/fpdf_merge.php(615): FPDF_Merge-&gt;getObject(Resource id #23, Array, '1') #2 /home/content/.../html/pdf2/fill.php(79): FPDF_Merge-&gt;add('qaf.pdf') #3 {main} thrown in /home/content/.../html/pdf2/fpdf_merge.php on line 74 
  </code></pre>
  
  <p>I'm guessing this might be due to my qaf.pdf being 7 pages long, but haven't found any documentation on that. </p>
  
  <p>So my questions are:</p>
  
  <p>1) What can I do to make fpdf_merge.php work with my two documents?</p>
  
  <p>2) Why is my email process not working when I use both the 1st and 2nd processes? </p>
  
  <p>I'm guessing I have things out of order somehow. But I don't know how to troubleshoot that in php. </p>
  
  <p>Thanks for any clues!! </p>
  
  <p>Note: I removed comments (other people's fine work), just to make it easier to read this post. They are retained in my working script.</p>
  
  <p>Cross-posted here: <a href="http://www.fpdf.org/phorum/read.php?f=1&amp;i=60395&amp;t=60395" rel="nofollow">http://www.fpdf.org/phorum/read.php?f=1&amp;i=60395&amp;t=60395</a></p>
  
  <p>1:</p>
  
  <pre><code>$s = new signature; 
  $s-&gt;use_template_pdf('signed','sig'); 
  $s-&gt;delete_signature(); 
  class signature { 
  var $pdf; 
  var $hash; 
  var $image; 
  function __construct(){ 
  require('fpdf/fpdf.php'); 
  require('fpdi/fpdi.php'); 
  $this-&gt;make_hash(); 
  $this-&gt;make_signature(); 
  } 
  function make_hash(){ 
  $this-&gt;hash = uniqid(); 
  } 
  function make_signature(){ 
  if(isset($_POST['img'])){ 
  $arr = explode(',',$_POST['img']); 
  $this-&gt;image = "image{$this-&gt;hash}.png"; 
  if(is_writable(dirname(__FILE__))){ 
  file_put_contents($this-&gt;image, base64_decode($arr[1])); 
  }else{ 
  die('&lt;p&gt;The working directory is not writable, abort.&lt;/p&gt;'); 
  } 
  } 
  } 
  function delete_signature(){ 
  if(file_exists($this-&gt;image)) @unlink($this-&gt;image); 
  } 
  function use_template_pdf($filename, $template){ 
  $this-&gt;pdf = new FPDI(); 
  
  $pagecount = $this-&gt;pdf-&gt;setSourceFile($template.'.pdf'); 
  $tplidx = $this-&gt;pdf-&gt;importPage(1); 
  $this-&gt;pdf-&gt;addPage(); 
  $this-&gt;pdf-&gt;SetXY(90, 260); 
  $this-&gt;pdf-&gt;Write(5,"Signature: "); 
  $this-&gt;pdf-&gt;useTemplate($tplidx, 10, 10); 
  $this-&gt;pdf-&gt;Image($this-&gt;image,125,260,-200); 
  if(is_writable(dirname(__FILE__))){ 
  $this-&gt;pdf-&gt;Output($filename.'.pdf', 'F'); 
  }else{ 
  $this-&gt;pdf-&gt;Output($filename.'.pdf', 'I'); 
  } 
  $this-&gt;pdf = NULL; 
  } 
  } 
  </code></pre>
  
  <p>2:</p>
  
  <pre><code>require('fpdm/fpdm.php'); 
  $fields = array( 
  'cascade' =&gt; $_POST['_fid_6'], 
  'structuretype' =&gt; $_POST['_fid_7'], 
  'marketmanager' =&gt; $_POST['_fid_8'], 
  ); 
  $pdf = new FPDM('template3.pdf'); 
  $pdf-&gt;Load($fields, false); 
  $pdf-&gt;Merge(); 
  $pdf-&gt;Output('qaf.pdf', 'F'); 
  </code></pre>
  
  <p>3:</p>
  
  <pre><code>require('fpdf_merge.php'); 
  $merge = new FPDF_Merge(); 
  $merge-&gt;add('qaf.pdf'); 
  $merge-&gt;add('signed.pdf'); 
  $merge-&gt;output('qaf-final.pdf', 'F'); 
  </code></pre>
  
  <p>4:</p>
  
  <pre><code>$to = $_POST['_fid_6']; 
  $from = "qaf@me.com"; 
  $subject = "Your QAF"; 
  $separator = md5(time()); 
  $eol = PHP_EOL; 
  $filename = "qaf-final.pdf"; 
  $pdfdoc = $pdf-&gt;Output("", "S"); 
  $attachment = chunk_split(base64_encode($pdfdoc)); 
  $headers = "From: ".$from.$eol; 
  $headers .= "MIME-Version: 1.0".$eol; 
  $headers .= "Content-Type: multipart/mixed; boundary=\"".$separator."\""; 
  $body = "--".$separator.$eol; 
  $body .= "Content-Transfer-Encoding: 7bit".$eol.$eol; 
  $body .= "Here is your QAF.".$eol; 
  $body .= "--".$separator.$eol; 
  $body .= "Content-Type: application/octet-stream; name=\"".$filename."\"".$eol; 
  $body .= "Content-Transfer-Encoding: base64".$eol; 
  $body .= "Content-Disposition: attachment".$eol.$eol; 
  $body .= $attachment.$eol; 
  $body .= "--".$separator."--"; 
  mail($to, $subject, $body, $headers);
  </code></pre>