fix: resolve Korean font display issue#169
Conversation
|
Thanks for raising this PR and reporting the issue for Korean chars in md_to_pdf command.
|
|
Thanks for the review—the feedback is super helpful! I’ve gone ahead and adjusted the PR as requested: Dropped pymupdf: Removed it from dev dependencies and deleted the text-extraction tests. Kept it lightweight: The core Hangul detection and CSS injection remain intact. Tests now only verify that the expected CSS is added and the PDF compiles. I agree with your broader concerns. Mixed CJK content is definitely complex and might need a different strategy down the road. For now, this targets the specific Korean-only case without adding heavy dependencies. Thanks again for taking a look! |
When converting Korean-only Markdown to PDF via the md_to_pdf tool (used in Dify workflows), Korean characters were rendered incorrectly — often as blank boxes or missing text — while English content displayed normally.
Example reported by a user:
ㅁㅁㅁㅁ >> 가나다라
Expected: Korean text visible in the PDF
Actual: Korean text missing or garbled
This issue originates in markdown-exporter, not in Dify. Dify only passes Markdown to the plugin; PDF generation and font handling happen inside this project.
Root Cause
No Korean detection — PDF font support was only enabled when Chinese or Japanese characters were detected.
Wrong fonts for Korean — Even when CJK fonts were applied, fonts like STSong-Light do not reliably render Hangul in PDF viewers.
Incorrect CSS — The entire comma-separated font list was wrapped in a single quoted string (treated as one invalid font name), and styles were applied only to the html element, not all text elements.
Changes Made
Core fix
md_exporter/utils/text_utils.py
Added KOREAN_CHAR_PATTERN and contains_korean() to detect Hangul (syllables, Jamo, compatibility Jamo, etc.)
md_exporter/utils/init.py
Exported contains_korean
md_exporter/services/svc_md_to_pdf.py
Added _build_cjk_font_families() to select fonts based on detected scripts:
Korean → HYSMyeongJo-Medium, HYGoThic-Medium
Chinese → STSong-Light, MSung-Light
Japanese → HeiseiMin-W3
Enabled CJK font injection when Korean text is present
Fixed CSS font-family syntax and applied styles to all text elements (p, h1–h6, li, etc.)
Tests
test/resources/example_md_korean_only.md — Korean test fixture
test/skills/test_text_utils.py — Unit tests for contains_korean()
test/skills/test_md_to_pdf.py — Extended with test_md_to_pdf_korean
test/utils/pdf_test_utils.py — PDF text extraction helper (PyMuPDF)
test/conftest.py — Pytest path configuration
pyproject.toml — Added pymupdf as a dev dependency for accurate PDF verification
Result
Pure Korean Markdown now converts to PDF with correctly rendered Hangul text. All 9 related tests pass.
uv sync --group dev
uv run pytest test/skills/test_text_utils.py test/skills/test_md_to_pdf.py -v