Setasign / FPDI

FPDI is a collection of PHP classes facilitating developers to read pages from existing PDF documents and use them as templates in FPDF.
https://www.setasign.com/fpdi
MIT License
1.09k stars 159 forks source link

FPDI - Getting "CrossReferenceException::OBJECT_NOT_FOUND" while trying to ImportPage from existing PDF file #191

Closed ali-assaabloy closed 12 months ago

ali-assaabloy commented 1 year ago

I'm trying to use the FPDI to add pages from the existing PDF file to new one and for that I wrote this code:

$pdf = new Fpdi();
$pagecount = $pdf->SetSourceFile('existingFileName.pdf');
for($i = 1; $i <= $pagecount; $i++){
    $pdf->AddPage();

    $tplId = $pdf->ImportPage($i);

    $pdf->useTemplate($tplId);
}

$pdf->Output('newfile.pdf', 'F');

But for some pages I'm getting the "Object (id:38521) not found." on ImportPage() :

public function getIndirectObject($objectNumber)
    {
        $offset = $this->getOffsetFor($objectNumber);
        if ($offset === false) {
            throw new CrossReferenceException(
                \sprintf('Object (id:%s) not found.', $objectNumber),
                CrossReferenceException::OBJECT_NOT_FOUND
            );
        }
...

existingFileName.pdf