Fix Font::getDetails() TypeError when Encoding is indirect reference to PDFObject#823
Open
kernie wants to merge 1 commit into
Open
Fix Font::getDetails() TypeError when Encoding is indirect reference to PDFObject#823kernie wants to merge 1 commit into
kernie wants to merge 1 commit into
Conversation
…malot#822) When a font's /Encoding entry is an indirect object reference that resolves to a plain PDFObject (encoding dictionary without /Type /Encoding), the previous unconditional (string) cast failed because PDFObject has no __toString() method. Fix: check the type before casting — - PDFObject (covers Encoding subclass and plain dicts): extract BaseEncoding name, fall back to 'Ansi' consistent with Encoding::getDetails() - Element (direct name like /WinAnsiEncoding): cast as before - null (no entry): return 'Ansi' as before Adds 5 unit tests covering all branches and one integration test with a minimal reproducer PDF (samples/bugs/EncodingAsIndirectPDFObject.pdf). fixes smalot#822
Collaborator
|
Thank you @kernie! I hope there is a German holiday soon, so I have some time to get on this 😅 Its looks very polished already at first glance. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Type of pull request
About
fixes #822
Font::getDetails()threw"PDFObject could not be converted to string"when a font's/Encodingentry was an indirect reference resolving to a plainPDFObject(encoding dictionary without/Type /Encoding). This is a valid PDF structure per spec Table 5.11.Root cause:
Font::getDetails()applied an unconditional(string)cast to the result of$this->get('Encoding').Header::resolveXRef()can return aPDFObject(which has no__toString()), causing aTypeErrorin PHP 8+.Fix: Check the type before casting:
PDFObject(covers bothEncodingsubclass and plain encoding dictionaries): extractBaseEncodingname; fall back to'Ansi'if absent — consistent withEncoding::getDetails()['BaseEncoding'].Element(direct name like/WinAnsiEncoding): cast as before.null(no entry): return'Ansi'as before.Changes:
src/Smalot/PdfParser/Font.php— fix ingetDetails()tests/PHPUnit/Unit/FontTest.php— 5 unit tests covering all branchestests/PHPUnit/Integration/FontTest.php— integration test with sample PDFsamples/bugs/EncodingAsIndirectPDFObject.pdf— minimal reproducer (720 B)AI disclosure
This PR was developed with Claude Code assistance. All code has been reviewed as best I can. I tested the code in my local installation.