Skip to content

Font::getDetails() throws TypeError when Encoding is indirect reference to PDFObject #822

Description

@kernie
  • PHP Version: 8.x (reproduced on 8.1–8.5)
  • PDFParser Version: 2.12.4+

Description:

Font::getDetails() throws a TypeError when a font's /Encoding entry is stored as an indirect object reference (N 0 R) that resolves to a plain PDFObject — i.e. an encoding dictionary that lacks /Type /Encoding.

This is a valid PDF structure per PDF spec Table 5.11: an encoding dictionary may carry only a /Differences array and omit /Type and /BaseEncoding.

Header::resolveXRef() correctly resolves the reference to a PDFObject. However, Font::getDetails() then executes (string) $this->get('Encoding'), which fails because PDFObject has no __toString() method.

Error:

Object of class Smalot\PdfParser\PDFObject could not be converted to string
in Font.php:97

PDF input

Minimal reproducer (included in the fix PR under samples/bugs/EncodingAsIndirectPDFObject.pdf).

The relevant PDF structure:

4 0 obj
<< /Type /Font /Subtype /Type1 /BaseFont /Helvetica /Encoding 5 0 R >>
endobj

5 0 obj
<< /Differences [32 /space 65 /A /B /C /D /E /F /G /H /I /J] >>
endobj

Expected output & actual output

Expected: Font::getDetails() returns ['Encoding' => 'Ansi', ...] (or the BaseEncoding name if present) without throwing.

Actual: TypeError: Object of class Smalot\PdfParser\PDFObject could not be converted to string

Code

$parser = new \Smalot\PdfParser\Parser();
$pdf    = $parser->parseFile('EncodingAsIndirectPDFObject.pdf');

foreach ($pdf->getPages() as $page) {
    foreach ($page->getFonts() as $font) {
        $font->getDetails(); // throws TypeError
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions