title: TCPDF and FPDI with multiple pages
body: <p>This looks like the simplest thing but I can't get it to work.</p>
  
  <p>I need to add text to the first page of a multi-page pdf (could be any number of pages)</p>
  
  <p>Using this code on a two page pdf (without the for loop, just using $pdf->importPage(2)) I end up with two pages but the second page is a repeat of page one. The text is written on the first page only which is good but I need all pages included in the output pdf. Here is my code</p>
  
  <pre><code>// Original file with multiple pages 
  $fullPathToFile = 'full/path/to/file.pdf';
  
  class PDF extends FPDI {
  
      var $_tplIdx;
  
      function Header() {
  
          global $fullPathToFile;
  
          if (is_null($this-&gt;_tplIdx)) {
  
              $this-&gt;setSourceFile($fullPathToFile);
              $this-&gt;_tplIdx = $this-&gt;importPage(1);
  
          }
          $this-&gt;useTemplate($this-&gt;_tplIdx);
  
      }
  
      function Footer() {}
  
  }
  
  // initiate PDF
  $pdf = new PDF();
  $pdf-&gt;setFontSubsetting(true);
  
  
  // add a page
  $pdf-&gt;AddPage();
  
  // The new content
  $pdf-&gt;SetFont("helvetica", "B", 14);
  $pdf-&gt;Text(10,10,'Some text here');
  
  // How to get the number of pages of original pdf???
  // $numPages = $pdf-&gt;getNumPages(???);
  
  // Carry on adding all remaining pages starting from page 2
  for($i=2;$i&lt;=$numPages;$i++) {
      // Add another page
      $pdf-&gt;AddPage();
      // Do I need to declare the source file here?
      // $pdf-&gt;setSourceFile($fullPathToWD);
      $pdf-&gt;importPage($i);
  }
  
  // Output the file as forced download
  $pdf-&gt;Output('theNewFile.pdf', 'D');
  </code></pre>
  
  <p><strong>Links to docs</strong></p>
  
  <p>TCPDF Classes
  <a href="http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced" rel="nofollow">http://www.tcpdf.org/doc/code/classTCPDF.html#a5171e20b366b74523709d84c349c1ced</a></p>
  
  <p>FPDI Classes
  <a href="http://www.setasign.de/support/manuals/fpdi/" rel="nofollow">http://www.setasign.de/support/manuals/fpdi/</a></p>
  
  <p>FPDF_TPL Classes
  <a href="http://www.setasign.de/support/manuals/fpdf-tpl/" rel="nofollow">http://www.setasign.de/support/manuals/fpdf-tpl/</a></p>