Merging PDF files with PHP/FPDI

pgee70 picture pgee70 · Mar 14, 2014 · Viewed 9.3k times · Source

i am trying to merge two files using FPDI the error i get is:'TCPDF ERROR: File is encrypted!', however, the files are not encrypted, at least the files are printable, viewable etc and no password is required.

i want to merge two files:

http://www.nps.org.au/__data/cmi_pdfs/CMI7412.pdf http://www.nps.org.au/__data/cmi_pdfs/CMI6656.pdf

after i copy the files to the server and store the file names in array ($files) that has the absolute file paths, my code is:

if (count ($files) > 0 )
{
    $pdf = new FPDI();
    $pdf->setPrintHeader(FALSE);
    $pdf->setPrintFooter(FALSE);
    foreach ($files as $file)
    {
        for ($i = 0; $i < count($files); $i++ )
        {
            $pagecount = $pdf->setSourceFile($files[$i]);
            for($j = 0; $j < $pagecount ; $j++)
            {
                $tplidx = $pdf->importPage(($j +1), '/MediaBox');
                $specs = $pdf->getTemplateSize($tplidx);
                if ( $specs['h'] > $specs['w'] )
                {
                    $orientation = 'P';
                }
                else
                {
                    $orientation = 'L';
                }
                $pdf->addPage($orientation,'A4');
                $pdf->useTemplate($tplidx, 0, 0, 0, 0, TRUE);
            }
        }
        $output = $pdf->Output('', 'S');
        foreach ( $files as $file )
        {
            delete_file($file);
        }
    }

I have also tried to merge the files using ghostscript, but with no luck. I tried acrobat pro, which required a password for one file, but when I used mac preview, i exported the file and was able to merge it using acrobat with no issues. i.e. mac preview removed the protection with no problems. So, what is it about the file CMI7412.pdf that stops merging, but not exporting, viewing, printing? and how can i get around it?

Answer

Kevin Chui picture Kevin Chui · Apr 10, 2015

I have tried similar issue and works fine, try it. It can handle different orientations between PDFs.

    // array to hold list of PDF files to be merged
    $files = array("a.pdf", "b.pdf", "c.pdf");
    $pageCount = 0;
    // initiate FPDI
    $pdf = new FPDI();

    // iterate through the files
    foreach ($files AS $file) {
        // get the page count
        $pageCount = $pdf->setSourceFile($file);
        // iterate through all pages
        for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
            // import a page
            $templateId = $pdf->importPage($pageNo);
            // get the size of the imported page
            $size = $pdf->getTemplateSize($templateId);

            // create a page (landscape or portrait depending on the imported page size)
            if ($size['w'] > $size['h']) {
                $pdf->AddPage('L', array($size['w'], $size['h']));
            } else {
                $pdf->AddPage('P', array($size['w'], $size['h']));
            }

            // use the imported page
            $pdf->useTemplate($templateId);

            $pdf->SetFont('Helvetica');
            $pdf->SetXY(5, 5);
            $pdf->Write(8, 'Generated by FPDI');
        }
    }