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
31 changes: 21 additions & 10 deletions src/Smalot/PdfParser/PDFObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -775,16 +775,27 @@ public function getTextArray(?Page $page = null): array
break;

case 'Do':
if (null !== $page) {
$args = preg_split('/\s/s', $command[self::COMMAND]);
$id = trim(array_pop($args), '/ ');
$xobject = $page->getXObject($id);

// @todo $xobject could be a ElementXRef object, which would then throw an error
if (\is_object($xobject) && $xobject instanceof self && !\in_array($xobject->getUniqueId(), self::$recursionStack, true)) {
// Not a circular reference.
$text[] = $xobject->getText($page);
}
if (is_null($page)) {
break;
}

$args = preg_split('/\s/s', $command[self::COMMAND]);
$id = trim(array_pop($args), '/ ');
$xobject = $page->getXObject($id);

// Check we got a PDFObject back.
if (!$xobject instanceof self) {
break;
}

// If the PDFObject is an image, do nothing, as images aren't text.
if ($xobject instanceof Image) {
break;
}

// Check this is not a circular reference.
if (!\in_array($xobject->getUniqueId(), self::$recursionStack, true)) {
$text[] = $xobject->getText($page);
}
break;

Expand Down
39 changes: 39 additions & 0 deletions tests/PHPUnit/Unit/PDFObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@

use PHPUnitTests\TestCase;
use Smalot\PdfParser\Document;
use Smalot\PdfParser\Element;
use Smalot\PdfParser\Element\ElementArray;
use Smalot\PdfParser\Header;
use Smalot\PdfParser\Page;
use Smalot\PdfParser\PDFObject;
use Smalot\PdfParser\XObject\Image;

class PDFObjectTest extends TestCase
{
Expand All @@ -22,4 +26,39 @@ public function testGetTextOnPageWithoutContent(): void

static::assertSame(' ', (new PDFObject($document, null, null))->getText(new Page($document)));
}

public function testTextArrayObjects(): void
{
$document = new Document();
$document->init();

$image = new Image($document);
$xObject = new PDFObject($document);

$header1 = new Header([
'Resources' => new Header([
'XObject' => new Header([
'Im0' => $image,
])
]),
'Contents' => new ElementArray([new Element('/Imo Do', $document)], $document),
]);
$page1 = new Page($document, $header1);

$header2 = new Header([
'Resources' => new Header([
'XObject' => new Header([
'Ps0' => $xObject,
])
]),
'Contents' => new ElementArray([new Element('/Ps0 Do', $document)], $document),
]);
$page2 = new Page($document, $header2);

// Page 1 contains an image, which should not appear in the text array.
self::assertSame([], $page1->getTextArray());

// Page 2 contains a non-image object, which should appear in the text array.
self::assertSame([' '], $page2->getTextArray());
}
}
Loading