phppdf

Merge multiple PDF files using PHP


SCENARIO: On my web application, I have some PDF files that the user can pick to merge to create a single file with all the pages together.

EXPECTED RESULT: A single PDF file


How can I solve such a problem in PHP? Are there any libraries I could use to do so? Do I need to use some specific PHP framework, or some specific PHP version? Thanks in advance


Solution

  • I've done this before. I had a pdf that I generated with fpdf, and I needed to add on a variable amount of PDFs to it.

    So I already had an fpdf object and page set up (http://www.fpdf.org/) And I used fpdi to import the files (http://www.setasign.de/products/pdf-php-solutions/fpdi/) FDPI is added by extending the PDF class:

    class PDF extends FPDI
    {
    
    } 
    
    
    
        $pdffile = "Filename.pdf";
        $pagecount = $pdf->setSourceFile($pdffile);  
        for($i=0; $i<$pagecount; $i++){
            $pdf->AddPage();  
            $tplidx = $pdf->importPage($i+1, '/MediaBox');
            $pdf->useTemplate($tplidx, 10, 10, 200); 
        }
    

    This basically makes each pdf into an image to put into your other pdf. It worked amazingly well for what I needed it for.