PHP and FPDI/FPDF: Fatal error: Uncaught Exception: FPDF error: Incorrect output destination

PHP and FPDI/FPDF: Fatal error: Uncaught Exception: FPDF error: Incorrect output destination

我有一个 PDF 文件,我想添加一个 FPDI/FPDF

的新页面

Fatal error: Uncaught Exception: FPDF error: Incorrect output destination: outfiles/111111.pdf in C:\wamp\www\pdf\fpdi\fpdf.php on line 271 ( ! ) Exception: FPDF error: Incorrect output destination: outfiles/111111.pdf in C:\wamp\www\pdf\fpdi\fpdf.php on line 271

require_once('fpdi/fpdf.php');
require_once('fpdi/fpdi.php');

foreach(glob('infiles/*.pdf') as $file)
{
    $filename = basename($file);
    $fileout = 'outfiles/' . $filename;
    //echo $fileout;
    $out = new FPDI();

    $pagecount = $out->setSourceFile($file);

    for($i = 1; $i <= $pagecount; $i++)
    {
        $tpl = $out->importPage($i); 

        $out->addPage($format);
        $out->useTemplate($tpl);

        if($i < $pagecount)
        {
            $out->addPage($format);
        }
    }

    $out->Output($fileout);
}

Output() 方法要求第一个参数是目标,snd 参数是文件名。

所以只需将行更改为:

$out->Output('F', $fileout);

此外,您应该知道不能使用 FPDI 修改 PDF 文档,但可以将页面导入可重复使用的结构。您生成的 PDF 是一个全新的 PDF 文档,您没有向原始文档添加新页面。