Merging PDF files with PHP
<p>I have several PDF files created dynamically using TCPDF. </p>

<p>I have to merge those PDF's created by TCPDF into one, and as I saw best practice is to do that with FPDI library. </p>

<p>All PDF's that have to be merged are stored in same directory.</p>

<p>To merge them, I'm using next code:</p>

<pre><code>require( MY_APP_PATH . 'fpdf/fpdf.php');
require( MY_APP_PATH . 'fpdi/fpdi.php');


$fpdi = new FPDI();
// iterate over array of files and merge
foreach ($filesToMerge as $file) {
    $fpdi-&gt;setSourceFile(MY_APP_PATH . 'pdf/' . $file);
    $tpl = $fpdi-&gt;importPage(1, '/MediaBox');
    $fpdi-&gt;addPage();
    $fpdi-&gt;useTemplate($tpl);
}

$fpdi-&gt;Output('F', 'merged.pdf');
</code></pre>

<p>Error I'm getting here is:</p>

<pre><code>TCPDF ERROR: Incorrect output destination: /VAR/WWW/HTML/MYAPP/PDF/MERGED.PDF
</code></pre>

<p>Looks like there is some collision between TCPDF and FPDI libraries (or even FPDF?), since they both have have same method Output.</p>

<p>Also, it works fine if I run it in separate code (without including TCPDF class)</p>

<p>Can you give me some idea how to avoid this and merge my PDF's?</p>