Search code examples
phppdffpdffpdi

FPDF: Mergin 2 PDF files into 1 PDF with 1 page


I'm trying to merge 2 PDFs into 1 PDF with only one page. Every PDF has 1 page.

Thanks to this post http://www.setasign.com/products/fpdi/about/ I was able to make the first PDF the template. Only Problem now is how do I import the other and set it as content without getting a PDF with multiple pages as result.

I hope somebody can help.

Thanks in advance.


Solution

  • An imported page doesn't mean that it results in a new page in your document until you write the code for this behaviour. Taking the example on the linked page you can import another documents page this way:

    <?php
    require_once('fpdf.php');
    require_once('fpdi.php');
    
    $pdf = new FPDI();
    
    $pdf->setSourceFile("Fantastic-Speaker.pdf");
    $tplIdxA = $pdf->importPage(1, '/MediaBox');
    
    $pdf->setSourceFile("Another-Fantastic-Speaker.pdf");
    $tplIdxB = $pdf->importPage(1, '/MediaBox');
    
    $pdf->addPage();
    // place the imported page of the first document:
    $pdf->useTemplate($tplIdxA, 10, 10, 90);
    // place the imported page of the snd document:
    $pdf->useTemplate($tplIdxB, 100, 10, 90);
    
    $pdf->Output();