Skip to content

Comments

Bugfix - Type resolve missing for nested pages in structure#227

Merged
JanSlabon merged 2 commits intoSetasign:masterfrom
denikod:bugfix/nested-pages
Jan 15, 2025
Merged

Bugfix - Type resolve missing for nested pages in structure#227
JanSlabon merged 2 commits intoSetasign:masterfrom
denikod:bugfix/nested-pages

Conversation

@denikod
Copy link
Contributor

@denikod denikod commented Dec 19, 2024

I found a bug in the code when working with PDF files that have a complex nested page structure.

Screenshot of an example structure

On the left is a complex structure that causes an error
On the right is a normal one

pdf-complex-nested

Which caused the following error:
Fatal error: Uncaught exception 'setasign\Fpdi\PdfParser\Type\PdfTypeException' with message 'Array value expected.'

The proposed commit fixes this bug.

@JanSlabon
Copy link
Member

Great catch and I really wonder why this issue was not triggered before. Can you share the PDF in question? There's already a test document for a name tree:

$path . '/10000_with-tree.pdf',

...but yours seems to be structured differently.

@denikod
Copy link
Contributor Author

denikod commented Dec 25, 2024

Can you share the PDF in question?

Hello,
Sorry for delay with response...
I cannot provide the exact document that caused the issue, but after some manipulations, I created an identical document with the same problem.
I have attached the problematic document here:
PDF-complex-structure.pdf

Example of PHP code causing an error
<?php

use setasign\Fpdi\Fpdi;

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

class Pdf {

	function generatePreview($file)
	{
		$pdf = new FPDI();
		
		$pdf_handle = fopen($file, 'rb');
		$pageCount = $pdf->setSourceFile($pdf_handle);

		$max = min(5, $pageCount);

		for ($i=1; $i <= $max; $i++)
		{
			//  Add page to the document
			$templateID = $pdf->importPage($i);

			$pdf->addPage();
			$pdf->useTemplate($templateID);
		}

		$pdf_content = $pdf->Output('S');

		header("Content-type:application/pdf");
		echo $pdf_content;

		fclose($pdf_handle);

		return;
	}
}

$pdf = new Pdf();
$pdf->generatePreview("PDF-complex-structure.pdf");

I hope I've contributed to the growth of this wonderful library! 😊
🎄 Merry Christmas!

@JanSlabon JanSlabon merged commit b866b5a into Setasign:master Jan 15, 2025
13 checks passed
@JanSlabon
Copy link
Member

Thanks! This test document is really a very great example of how not to create a page tree 😂👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants