【问题标题】:Merging PDF files with PHP/FPDI使用 PHP/FPDI 合并 PDF 文件
【发布时间】:2014-04-19 17:17:53
【问题描述】:

我正在尝试使用FPDI 合并两个文件,我得到的错误是:'TCPD 错误:文件已加密!',但是,文件未加密,至少文件是可打印、可查看等且没有密码是必需的。

我想合并两个文件:

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

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

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 preview 时,我导出了文件并且能够使用 acrobat 合并它而没有问题。即 mac preview 删除了没有问题的保护。 那么,停止合并但不能导出、查看、打印的文件 CMI7412.pdf 是怎么回事?我该如何解决?

【问题讨论】:

    标签: php pdf pdf-generation fpdi


    【解决方案1】:

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

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

    【讨论】:

    • getTemplateSize 已更改。它返回widthheight,而不是wh,现在它也返回orientation,所以你不需要检查宽度是否更大。只需使用$pdf-&gt;AddPage($size['orientation'], array($size['width'], $size['height']));
    • 此外,在将页面添加到新 PDF 时,FPDI/FPDF 本身不会保留注释(例如超链接)。如果要保留超链接,请使用以下内容:pastebin.com/vKWsH61W
    【解决方案2】:

    问题在于 pdf 文件中的加密,它受到保护,无需密码即可更改。

    我使用qpdf 将解密后的 pdf 版本导出为临时文件。然后我使用pdftk 加入文件。结果比 PHP 库快得多。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-18
      • 2016-08-10
      • 2023-04-09
      • 2011-06-15
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      • 1970-01-01
      相关资源
      最近更新 更多