title: Using fpdf to modify existing pdf in php
body: <p>I have a bank of pdfs on my server which when downloaded need text appended to every page. I am using fpdf to try and open the file, append the text to each page, close the file and serve to the browser.</p>
  
  <pre><code>$pdf = new FPDI();
  
  $pdf-&gt;setSourceFile($filename); 
  // import page 1 
  $tplIdx = $pdf-&gt;importPage(1); 
  //use the imported page and place it at point 0,0; calculate width and height
  //automaticallay and ajust the page size to the size of the imported page 
  $pdf-&gt;useTemplate($tplIdx, 0, 0, 0, 0, true); 
  
  // now write some text above the imported page 
  $pdf-&gt;SetFont('Arial', '', '13'); 
  $pdf-&gt;SetTextColor(0,0,0);
  //set position in pdf document
  $pdf-&gt;SetXY(20, 20);
  //first parameter defines the line height
  $pdf-&gt;Write(0, 'gift code');
  //force the browser to download the output
  $pdf-&gt;Output('gift_coupon_generated.pdf', 'D');
  
  header("location: ".$filename);
  </code></pre>
  
  <p>At the minute this just tries to put some text anywhere on the pdf and save it but I get the error</p>
  
  <pre><code>FPDF error: You have to add a page first!
  </code></pre>
  
  <p>If I can get it to do this I then need it to append the text to every page in the document rather than just 1, not sure how to do this having read the documentation</p>