- 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
}
}
Description:
Font::getDetails()throws aTypeErrorwhen a font's/Encodingentry is stored as an indirect object reference (N 0 R) that resolves to a plainPDFObject— 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
/Differencesarray and omit/Typeand/BaseEncoding.Header::resolveXRef()correctly resolves the reference to aPDFObject. However,Font::getDetails()then executes(string) $this->get('Encoding'), which fails becausePDFObjecthas no__toString()method.Error:
PDF input
Minimal reproducer (included in the fix PR under
samples/bugs/EncodingAsIndirectPDFObject.pdf).The relevant PDF structure:
Expected output & actual output
Expected:
Font::getDetails()returns['Encoding' => 'Ansi', ...](or theBaseEncodingname if present) without throwing.Actual:
TypeError: Object of class Smalot\PdfParser\PDFObject could not be converted to stringCode