使用PHP/FPDI合并PDF文件 [英] Merging PDF files with PHP/FPDI

查看:770
本文介绍了使用PHP/FPDI合并PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 FPDI 合并两个文件,我得到的错误是: "TCPDF错误:文件已加密!",但是,文件未加密,至少文件是可打印的,可见的,并且不需要密码.

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.

我要合并两个文件:

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

将文件复制到服务器并将文件名存储在具有绝对文件路径的数组($ files)中之后,我的代码是:

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

我也尝试过使用ghostscript合并文件,但是没有运气. 我尝试了acrobat pro,该文件需要一个文件的密码,但是当我使用mac预览时,我导出了文件,并能够使用acrobat合并它而没有任何问题.即Mac Preview毫无问题地删除了保护. 那么,关于文件CMI7412.pdf停止合并,而不是导出,查看,打印,该怎么办?以及我该如何解决?

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?

推荐答案

我已经尝试过类似的问题,并且工作正常,请尝试一下.它可以处理PDF之间的不同方向.

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

这篇关于使用PHP/FPDI合并PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆