fix(cli): write the --show-layout HTML export as UTF-8 - #4048
Open
Anai-Guo wants to merge 1 commit into
Open
Conversation
The `--show-layout` arm of the split-page HTML export builds the page with HTMLDocSerializer and writes it through a bare `open(fname, "w")`, which encodes with `locale.getpreferredencoding(False)`. On Windows that is a legacy code page (cp1252, cp936, cp932, ...), so exporting any document whose text is not representable there dies with UnicodeEncodeError. Every other export path already writes UTF-8 explicitly: the `else` arm of this very `if` calls `save_as_html()`, and each `save_as_*` in docling-core ends in `filename.write_text(..., encoding="utf-8")`. This one hand-rolled write is the only place in the package that inherits the locale encoding. Signed-off-by: Tai An <antai12232931@outlook.com>
Contributor
|
✅ DCO Check Passed Thanks @Anai-Guo, all your commits are properly signed off. 🎉 |
Contributor
Merge Protections🟢 Merge protection satisfied — ready to merge. Show 1 satisfied protection🟢 Enforce conventional commitMake sure that we follow https://www.conventionalcommits.org/en/v1.0.0/
|
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.
The
--show-layoutarm of the split-page HTML export writes the serialized page through a bareopen(fname, "w"):https://github.com/docling-project/docling/blob/main/docling/cli/main.py#L541
Text-mode
open()withoutencoding=encodes withlocale.getpreferredencoding(False). On Windows that is a legacy code page — cp1252 / cp936 / cp932 depending on the system locale — so converting any document whose text is not representable in that code page aborts at the very last step, after all the parsing and layout work is already done:Curly quotes, en/em dashes, bullets and accented Latin are enough to trigger it on cp1252; on cp936/cp932 anything outside the local script is.
Why this one line is the odd one out
Every other export path already writes UTF-8 explicitly:
elsearm of this veryifcallssave_as_html();fname.open("w", encoding="utf-8");docling-core, everysave_as_*bottoms out infilename.write_text(..., encoding="utf-8")— includingsave_as_htmland the DocTags writer, which persistsserializer.serialize().text, the same expression this branch writes.An AST sweep of the
docling/package finds no other text-modeopen()withoutencoding=; the remaining bareopencalls are alltarfile.open(..., "r:gz"). So this is the last site in the package that inherits the locale encoding, and it makes--show-layoutthe only HTML export that is not portable.This is the same failure mode as the previously reported #221, #230, #578, #579 and #598 — all
'charmap' codecerrors on Windows — surviving in a branch added later.Reproduction
Exercising the exact expression from the CLI against the real serializer, on Windows with
locale.getpreferredencoding(False) == 'cp1252', CPython 3.12.10,docling-core2.89.0:The fix is to pass
encoding="utf-8", matching the rest of the export code. Behaviour is unchanged on platforms whose preferred encoding is already UTF-8.Checklist:
🤖 Generated with Claude Code