Skip to content
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/EncodingAsIndirectPDFObject.pdf
Binary file not shown.
14 changes: 13 additions & 1 deletion src/Smalot/PdfParser/Font.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,19 @@ public function getDetails(bool $deep = true): array

$details['Name'] = $this->getName();
$details['Type'] = $this->getType();
$details['Encoding'] = ($this->has('Encoding') ? (string) $this->get('Encoding') : 'Ansi');
$encoding = $this->has('Encoding') ? $this->get('Encoding') : null;
if ($encoding instanceof PDFObject) {
// Encoding is an indirect reference to an encoding dictionary (PDF spec Table 5.11).
// Encoding extends PDFObject, so this branch handles both cases.
// Extract BaseEncoding name; absent means the font's built-in encoding is the base.
$baseEncoding = $encoding->getHeader()->get('BaseEncoding');
$baseEncodingStr = ($baseEncoding instanceof Element) ? (string) $baseEncoding : '';
$details['Encoding'] = $baseEncodingStr !== '' ? $baseEncodingStr : 'Ansi';
} elseif ($encoding instanceof Element) {
$details['Encoding'] = (string) $encoding;
} else {
$details['Encoding'] = 'Ansi';
}

$details += parent::getDetails($deep);

Expand Down
31 changes: 31 additions & 0 deletions tests/PHPUnit/Integration/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,4 +593,35 @@ public function testDecodeContentIssue549(): void
// check result
$this->assertEquals('foobar-', $font->decodeContent("foobar-\x8D"));
}

/**
* Font::getDetails() must not throw when a font's Encoding entry is an
* indirect object reference that resolves to a plain PDFObject instead of
* an Element — i.e. an encoding dictionary that lacks /Type /Encoding.
*
* This is a valid PDF structure per PDF spec Table 5.11: the dictionary
* may carry only a /Differences array and omit /Type and /BaseEncoding.
* Without the fix, PHP throws:
* "Object of class PDFObject could not be converted to string"
*
* @see https://github.com/smalot/pdfparser/issues/822
*/
public function testGetDetailsWithEncodingAsIndirectPDFObject(): void
{
$filename = $this->rootDir.'/samples/bugs/EncodingAsIndirectPDFObject.pdf';
$parser = $this->getParserInstance();
$document = $parser->parseFile($filename);

foreach ($document->getPages() as $page) {
foreach ($page->getFonts() as $font) {
// Must not throw "PDFObject could not be converted to string"
$details = $font->getDetails();
$this->assertIsString($details['Encoding']);
$this->assertNotEmpty($details['Encoding']);
}
}

// Text extraction must still work correctly
$this->assertSame('Hello', trim($document->getText()));
}
}
91 changes: 91 additions & 0 deletions tests/PHPUnit/Unit/FontTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,102 @@
use PHPUnitTests\TestCase;
use Smalot\PdfParser\Config;
use Smalot\PdfParser\Document;
use Smalot\PdfParser\Element;
use Smalot\PdfParser\Encoding;
use Smalot\PdfParser\Font;
use Smalot\PdfParser\Header;
use Smalot\PdfParser\PDFObject;

class FontTest extends TestCase
{
/**
* Font::getDetails() must not throw when Encoding is an indirect reference
* that resolves to a PDFObject instead of an Element.
*
* Such PDFs store the Encoding as an object reference (e.g. "12 0 R") whose
* resolved target is a plain PDFObject without /Type /Encoding — a valid
* structure per PDF spec Table 5.11 (encoding dictionary with /Differences).
*
* @see https://github.com/smalot/pdfparser/issues/822
*/
public function testGetDetailsEncodingAsPDFObjectWithBaseEncoding(): void
{
$document = new Document();
$encodingObj = new PDFObject(
$document,
new Header(['BaseEncoding' => new Element('WinAnsiEncoding')])
);
$font = new Font($document, new Header(['Encoding' => $encodingObj]));

$details = $font->getDetails(false);

self::assertSame('WinAnsiEncoding', $details['Encoding']);
}

/**
* When Encoding is a PDFObject without a BaseEncoding entry the font uses
* its built-in encoding as base (PDF spec §5.5.5). getDetails() must return
* 'Ansi' as fallback, consistent with Encoding::getDetails()['BaseEncoding'].
*/
public function testGetDetailsEncodingAsPDFObjectWithoutBaseEncoding(): void
{
$document = new Document();
$encodingObj = new PDFObject($document, new Header([]));
$font = new Font($document, new Header(['Encoding' => $encodingObj]));

$details = $font->getDetails(false);

self::assertSame('Ansi', $details['Encoding']);
}

/**
* When Encoding is an Encoding instance (PDFObject subclass, /Type /Encoding
* present) the BaseEncoding name must be returned.
*/
public function testGetDetailsEncodingAsEncodingInstance(): void
{
$document = new Document();
$encodingObj = new Encoding(
$document,
new Header(['BaseEncoding' => new Element('MacRomanEncoding')])
);
$font = new Font($document, new Header(['Encoding' => $encodingObj]));

$details = $font->getDetails(false);

self::assertSame('MacRomanEncoding', $details['Encoding']);
}

/**
* When Encoding is a direct name element (e.g. /WinAnsiEncoding) the name
* is returned as-is — the original pre-fix behaviour must be preserved.
*/
public function testGetDetailsEncodingAsDirectElement(): void
{
$document = new Document();
$font = new Font(
$document,
new Header(['Encoding' => new Element('WinAnsiEncoding')])
);

$details = $font->getDetails(false);

self::assertSame('WinAnsiEncoding', $details['Encoding']);
}

/**
* When no Encoding entry is present getDetails() must return 'Ansi'.
*/
public function testGetDetailsEncodingMissingDefaultsToAnsi(): void
{
$document = new Document();
$font = new Font($document, new Header([]));

$details = $font->getDetails(false);

self::assertSame('Ansi', $details['Encoding']);
}

/**
* decodeText must decode \b.
*
Expand Down
Loading