Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added samples/bugs/PullRequestDuplicateKids.pdf
Binary file not shown.
35 changes: 32 additions & 3 deletions src/Smalot/PdfParser/Document.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ public function getPages()
/** @var Pages $object */
$object = $catalogue->get('Pages');
if (method_exists($object, 'getPages')) {
return $object->getPages(true);
return $this->uniquePages($object->getPages(true));
}
}

Expand All @@ -415,19 +415,48 @@ public function getPages()
$pages = array_merge($pages, $object->getPages(true));
}

return $pages;
return $this->uniquePages($pages);
}

if ($this->hasObjectsByType('Page')) {
// Search for 'page' (unordered pages).
$pages = $this->getObjectsByType('Page');

return array_values($pages);
return $this->uniquePages(array_values($pages));
}

throw new MissingCatalogException('Missing catalog.');
}

/**
* @param array<Page> $pages
*
* @return array<Page>
*/
protected function uniquePages(array $pages): array
{
$unique = [];
$seen = [];

foreach ($pages as $page) {
if (!\is_object($page)) {
continue;
}

$id = \function_exists('spl_object_id')
? (string) \spl_object_id($page)
: \spl_object_hash($page);
if (isset($seen[$id])) {
continue;
}

$seen[$id] = true;
$unique[] = $page;
}

return $unique;
}

public function getText(?int $pageLimit = null): string
{
$texts = [];
Expand Down
39 changes: 39 additions & 0 deletions tests/PHPUnit/Integration/DocumentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
use PHPUnitTests\TestCase;
use Smalot\PdfParser\Document;
use Smalot\PdfParser\Header;
use Smalot\PdfParser\Parser;
use Smalot\PdfParser\Page;
use Smalot\PdfParser\Pages;
use Smalot\PdfParser\PDFObject;
Expand Down Expand Up @@ -233,6 +234,44 @@ public function testGetPagesMissingCatalog(): void
$document->getPages();
}

public function testGetPagesDeduplicatesDuplicateKidsReferences(): void
{
$document = $this->getDocumentInstance();

$content = '<</Type/Page>>';
$header = Header::parse($content, $document);
$page = $this->getPageInstance($document, $header);

$content = '<</Type/Pages/Kids[10 0 R 10 0 R]>>';
$header = Header::parse($content, $document);
$pagesNode = $this->getPagesInstance($document, $header);

$content = '<</Type/Catalog/Pages 20 0 R>>';
$header = Header::parse($content, $document);
$catalog = $this->getPDFObjectInstance($document, $header);

$document->setObjects([
'10_0' => $page,
'20_0' => $pagesNode,
'30_0' => $catalog,
]);

$pages = $document->getPages();

$this->assertCount(1, $pages);
$this->assertSame($page, $pages[0]);
}

/**
* @see https://github.com/smalot/pdfparser/pull/795
*/
public function testGetPagesDeduplicatesDuplicateKidsFixture(): void
{
$document = (new Parser())->parseFile($this->rootDir.'/samples/bugs/PullRequestDuplicateKids.pdf');

self::assertCount(1, $document->getPages());
}

/**
* @see https://github.com/smalot/pdfparser/issues/721
*/
Expand Down
Loading