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

fix: Fail to identify fonts (name and size) #629 #630

Merged
merged 3 commits into from
Aug 11, 2023
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
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]));
}
}