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.
Overview
Fixed an issue where certain PDF files would fail with
KeyError: 'china-ss'
due to incorrect font handling duringdoc_zh
construction.Problem Details
I noticed that certain PDF files result in errors. For example:
The following error occurs:
Investigation revealed that fonts were not being added correctly in the
translate_stream
function. The following debug code shows the difference in font handling between the problematic and working cases:Problematic case:
Working case:
Solution
To address this issue, I modified the code to save
doc_en
to a temporaryBytesIO
stream before creatingdoc_zh
. This approach was inspired by the existing pattern in the same function wheredoc_zh
is saved to aBytesIO
stream before callingtranslate_patch
. Applying the same pattern earlier in the process ensures proper font initialization and prevents theKeyError
while maintaining the original functionality.Testing
I have verified that: