diff --git a/doc/Usage.md b/doc/Usage.md index 3f0f8b27..37c65aff 100644 --- a/doc/Usage.md +++ b/doc/Usage.md @@ -76,8 +76,8 @@ $config->setDataTmFontInfoHasToBeIncluded(true); // use config and parse file $parser = new Smalot\PdfParser\Parser([], $config); $pdf = $parser->parseFile('document.pdf'); - -$data = $pdf->getPages()[0]->getDataTm(); +$firstpage = $pdf->getPages()[0]; +$data = $firstpage->getDataTm(); Array ( @@ -121,9 +121,8 @@ Text width should be calculated on text from dataTm to make sure all character w In next example we are using data from above. ```php -$fonts = $pdf->getFonts(); $font_id = $data[0][2]; //R7 -$font = $fonts[$font_id]; +$font = $firstpage->getFont($font_id); $text = $data[0][1]; $width = $font->calculateTextWidth($text, $missing); ``` diff --git a/samples/bugs/Issue629.pdf b/samples/bugs/Issue629.pdf new file mode 100644 index 00000000..be61f28a Binary files /dev/null and b/samples/bugs/Issue629.pdf differ diff --git a/src/Smalot/PdfParser/Page.php b/src/Smalot/PdfParser/Page.php index fbc19872..10afb523 100644 --- a/src/Smalot/PdfParser/Page.php +++ b/src/Smalot/PdfParser/Page.php @@ -712,8 +712,6 @@ public function getDataTm(array $dataCommands = null): array $Tl = $defaultTl; $Tx = 0; $Ty = 0; - $fontId = $defaultFontId; - $fontSize = $defaultFontSize; break; /* diff --git a/tests/PHPUnit/Integration/PageTest.php b/tests/PHPUnit/Integration/PageTest.php index e94723d4..7807101e 100644 --- a/tests/PHPUnit/Integration/PageTest.php +++ b/tests/PHPUnit/Integration/PageTest.php @@ -846,4 +846,47 @@ public function testIssue454(): void $textData = $page->getTextXY(67.5, 756.25); $this->assertStringContainsString('{signature:signer505906:Please+Sign+Here}', $textData[0][1]); } + + /** + * Check that BT and ET do not reset the font. + * + * Data TM font info is included. + * + * @see https://github.com/smalot/pdfparser/pull/630 + */ + public function testIssue629WithDataTmFontInfo(): void + { + $config = new Config(); + $config->setDataTmFontInfoHasToBeIncluded(true); + + $filename = $this->rootDir.'/samples/bugs/Issue629.pdf'; + $parser = $this->getParserInstance($config); + $document = $parser->parseFile($filename); + $pages = $document->getPages(); + $page = end($pages); + $dataTm = $page->getDataTm(); + + $this->assertCount(4, $dataTm[0]); + $this->assertEquals('F2', $dataTm[0][2]); + } + + /** + * Data TM font info is NOT included. + * + * @see https://github.com/smalot/pdfparser/pull/630 + */ + public function testIssue629WithoutDataTmFontInfo(): void + { + $config = new Config(); + + $filename = $this->rootDir.'/samples/bugs/Issue629.pdf'; + $parser = $this->getParserInstance($config); + $document = $parser->parseFile($filename); + $pages = $document->getPages(); + $page = end($pages); + $dataTm = $page->getDataTm(); + + $this->assertCount(2, $dataTm[0]); + $this->assertFalse(isset($dataTm[0][2])); + } }