Merging PDF files with PHP/FPDI

◇◆丶佛笑我妖孽 提交于 2019-12-21 21:40:43

问题


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?


回答1:


The problem was the encryption in the pdf file, it was protected from changes without a password.

I used qpdf to export a decrypted version of the pdf as a temporary file. Then I used pdftk to join the files. Turns out to be far faster than the PHP libraries.




回答2:


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');
        }
    }


来源:https://stackoverflow.com/questions/22404601/merging-pdf-files-with-php-fpdi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!