title: PHP - How to use mPDF to merge PDFs
body: <p>I will start out by saying that I can generate PDFs just fine with mPDF, but for the life of me, I can't get it to merge an existing PDF with the PDF it just generated.</p>
  
  <p>What I need to figure out is how to append/add the existing PDF to the newly generated PDF. I've tried using the mPDF methods for importing pages, but all I can get is an error like:      </p>
  
  <pre><code>mPDF error: Cannot open '/downloads/test.pdf'.
  </code></pre>
  
  <p>The above message is ambiguous and unclear as to WHY it can't open the file...
  Here's the code I'm using to try and merge PDFs:</p>
  
  <pre><code> include_once("./pdf/mpdf/mpdf.php");
  
   $output_file = $_GET['output_file'];
   $url = $_GET['input_file'];
   $technical_drawing = $_GET['tech_drawing'];
  
   $html = file_get_contents($url);
  
   $mpdf = new mPDF('utf-8','Letter','','',0,0,0,0,0,0,'P');
   $mpdf-&gt;SetImportUse(); 
  
   $pagecount = $mpdf-&gt;SetSourceFile($technical_drawing);
   $tplIdx = $mpdf-&gt;ImportPage($pagecount);
   $mpdf-&gt;UseTemplate($tplIdx);
  
   $mpdf-&gt;WriteHTML($html);
   $mpdf-&gt;Output($output_file, 'D');
  
   exit;
  </code></pre>
  
  <p>$output_file will be the filename shown to the user.
  $url is the HTML we're writing into the file during PDF generation.
  $technical_drawing is a relative path to a PDF that we want to append/merge with the generated PDF.</p>
  
  <p>I understand I could use something like ghostscript, but I don't have that type of access on the client's server.</p>
  
  <p>Let me know if anyone has found a solution to this using mPDF or if I'm S.O.L. and need to find another library to do PDF merging. I am really looking for solutions or suggestions, but not just links to another library. I've exhausted what I can find in Google or in mPDF's documentation that describes the error I'm having.</p>
  
  <p>EDIT: changed mPDF error from <a href="http://example.com/pdf/example.pdf">http://example.com/pdf/example.pdf</a> to '/downloads/test.pdf'.</p>
  
  <p>EDIT_2: Code has been fixed to take relative path.</p>
  
  <p>Here's final working code. Bonus if anyone knows how to specify the order of writing HTML to PDF document, importing page as last page (with custom page size different from that of the HTML).</p>
  
  <pre><code>    include_once("./pdf/mpdf/mpdf.php");
  
      $output_file = 'test-' . $_GET['output_file'];
      $url = $_GET['input_file'];
      $technical_drawing = $_GET['tech_drawing'];
  
      $html = file_get_contents($url);
  
  
      if(!file_exists($technical_drawing)) {
        $mpdf = new mPDF('utf-8','Letter','','',0,0,0,0,0,0,'L');
      } else {
        $mpdf = new mPDF('utf-8','A3-L','','',0,0,0,0,0,0,'L');
  
        $mpdf-&gt;SetImportUse(); 
  
        $pagecount = $mpdf-&gt;SetSourceFile($technical_drawing);
        $import_page = $mpdf-&gt;ImportPage();
  
        $mpdf-&gt;UseTemplate($import_page);
  
        // Add Last page
        $mpdf-&gt;AddPageByArray(array(
          'orientation' =&gt; 'P',
          'ohvalue' =&gt; 1,
          'ehvalue' =&gt; -1,
          'ofvalue' =&gt; -1,
          'efvalue' =&gt; -1,
          'newformat' =&gt; 'Letter'
        ));
      }
  
      $mpdf-&gt;WriteHTML($html);
      $mpdf-&gt;Output($output_file, 'D');
  
      exit;
  </code></pre>