Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getTextArray: Add trim check in Do command #762

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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/Issue761.pdf
Binary file not shown.
9 changes: 7 additions & 2 deletions src/Smalot/PdfParser/PDFObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -781,9 +781,14 @@ public function getTextArray(?Page $page = null): array
$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)) {
if (
\is_object($xobject)
&& $xobject instanceof self
&& !\in_array($xobject->getUniqueId(), self::$recursionStack, true)
&& $objectText = trim($xobject->getText($page))
) {
// Not a circular reference.
$text[] = $xobject->getText($page);
$text[] = $objectText;
}
}
break;
Expand Down
46 changes: 46 additions & 0 deletions tests/PHPUnit/Integration/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

namespace PHPUnitTests\Integration;

use PHPUnit\Framework\Attributes\Ticket;
use PHPUnitTests\TestCase;
use Smalot\PdfParser\Config;
use Smalot\PdfParser\Document;
Expand Down Expand Up @@ -958,4 +959,49 @@ public function testCmCommandInPdfs(): void
]
);
}

/**
* Test that the parser returns the correct text for the corresponding Text Matrix in getDataTm() if the document
* contains an 'Do' command with an empty string content.
*/
#[Ticket('https://github.com/smalot/pdfparser/pull/761')]
public function testDoCommandWithEmptyStringContentInPdf(): void
{
$config = new Config();
$parser = $this->getParserInstance($config);

// Document contains an 'Do' command with an empty string content
$document = $parser->parseFile($this->rootDir . '/samples/bugs/Issue761.pdf');

$pages = $document->getPages();
self::assertCount(2, $pages);

self::assertEquals(
[
[
[
'1',
'0',
'0',
'1',
'36.266',
'754.031',
],
'Test',
],
[
[
'1',
'0',
'0',
'1',
'34.016',
'701.653',
],
'{{signer1}}',
],
],
$pages[1]->getDataTm()
);
}
}
Loading