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

user3282262 picture user3282262 · Aug 18, 2017 · Viewed 8.2k times · Source

I have a PDF file, and I want to add a new page with 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);
}

Answer

Jan Slabon picture Jan Slabon · Aug 18, 2017

The Output() method requires the first parameter to be the destination and the snd parameter the filename.

So just change the line to:

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

Additionally you should know that you cannot modify a PDF document with FPDI but you import the pages into a reusable structure. Your resulting PDF is a completely new PDF document and you'd not added new pages to the original one.