Skip to content

Commit

Permalink
fix: Fail to identify fonts (name and size) #629 (#630)
Browse files Browse the repository at this point in the history
* fix: Fail to identify fonts (name and size) #629

* docs(fix): in sample code, get font from the Page rather than the PDF list, as font's IDs are different

* added two tests to prove changes in #630 are working

---------

Co-authored-by: Konrad Abicht <[email protected]>
  • Loading branch information
mbideau-atreal and k00ni authored Aug 11, 2023
1 parent eef0263 commit f3a5a3e
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
7 changes: 3 additions & 4 deletions doc/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
(
Expand Down Expand Up @@ -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);
```
Expand Down
Binary file added samples/bugs/Issue629.pdf
Binary file not shown.
2 changes: 0 additions & 2 deletions src/Smalot/PdfParser/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,8 +712,6 @@ public function getDataTm(array $dataCommands = null): array
$Tl = $defaultTl;
$Tx = 0;
$Ty = 0;
$fontId = $defaultFontId;
$fontSize = $defaultFontSize;
break;

/*
Expand Down
43 changes: 43 additions & 0 deletions tests/PHPUnit/Integration/PageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]));
}
}

0 comments on commit f3a5a3e

Please sign in to comment.