Skip to content

fix(book-pdf): prevent missing CJK text in generated PDFs#1080

Open
Mowmowj wants to merge 4 commits intof:mainfrom
Mowmowj:main
Open

fix(book-pdf): prevent missing CJK text in generated PDFs#1080
Mowmowj wants to merge 4 commits intof:mainfrom
Mowmowj:main

Conversation

@Mowmowj
Copy link

@Mowmowj Mowmowj commented Mar 18, 2026

Description

This MR fixes missing CJK(China, Japan, Korea) text in generated book PDFs.

The issue was that some Chinese text rendered correctly in HTML but disappeared in exported PDFs, especially in:

  • section headings
  • prompt/example blocks styled with .prompt-code
image

The root cause was that the generated print HTML relied on default Latin-oriented font stacks, and .prompt-code depended on a monospace stack that is often missing CJK coverage on local systems.

This change adds locale-aware font stacks for zh, ja, and ko, routes heading styles through a dedicated heading font, and makes .prompt-code use the locale sans stack for CJK locales instead of the monospace stack.

The fix is intentionally narrow:

  • no component structure changes
  • no PDF generation pipeline changes
  • no global removal of monospace styles
  • only locale-aware font selection in generated HTML/CSS

Further Suggestion

This PR proposal has good compatibility but does not use the mono fonts.
If still want to use mono font for the prompt/example blocks styled with .prompt-code, could install the relevent CJK fonts on the local device.

Testing

  • Ran:
node --import tsx scripts/generate-book-pdf.ts zh --print
  • Opened public/book-pdf/book-zh-print.html in Chrome and exported pages 14-15
  • Verified:
    • page 14 Chinese prompt/example text is visible
    • page 15 heading 提示词工程的发展历程 is visible
    • comparison blocks still render correctly
  • Confirmed the exported PDF text layer also contains the expected Chinese text

Type of Change

  • [ x ] Bug fix
  • Documentation update
  • Other (please describe):

⚠️ Want to Add a New Prompt?

Please don't edit prompts.csv directly!

Instead, visit prompts.chat and:

  1. Login with GitHub - Click the login button and authenticate with your GitHub account
  2. Create your prompt - Use the prompt editor to add your new prompt
  3. Submit - Your prompt will be reviewed and a GitHub Action will automatically create a commit on your behalf

This ensures proper attribution, formatting, and keeps the repository in sync. You'll also appear on the Contributors page!


Additional Notes

Summary by CodeRabbit

  • Style
    • Implemented locale-aware font selection for PDF output to better match language typography.
    • Headings now use a dedicated heading font for more consistent titles and TOC appearance.
    • Prompt/code blocks use a designated prompt font for improved readability.
    • Improved support for CJK languages and adjusted heading letter-spacing for better visual balance.

@vercel
Copy link

vercel bot commented Mar 18, 2026

@Mowmowj is attempting to deploy a commit to the fkadev Team on Vercel.

A member of the Team first needs to authorize it.

- Update Chinese (zh) font stacks for sans-serif and monospace fonts. - Prefer 'Hiragino Sans GB' and 'Noto Sans CJK SC' for better rendering. - Switch promptFont to use sans-serif font for CJK locales.
@coderabbitai
Copy link

coderabbitai bot commented Mar 19, 2026

📝 Walkthrough

Walkthrough

Adds locale-aware font stack selection to the PDF generation script and applies those stacks via CSS variables. Multiple heading and prompt elements now use locale-specific --font-heading/--font-prompt; h1 letter-spacing adjusted. Minor letconst change and comment updates in template rendering.

Changes

Cohort / File(s) Summary
Locale-Aware Font Selection
scripts/generate-book-pdf.ts
Added FontStacks type, DEFAULT_FONT_STACKS, LOCALE_FONT_STACKS, and getFontStacks(locale) with CJK detection. Updated generateHtmlDocument() to derive isCjk, select headingFont and promptFont, and set CSS variables --font-serif, --font-sans, --font-mono, --font-heading, and --font-prompt. Replaced prior fixed font-family uses: cover, TOC, chapter headings, h1h4, back-matter h2, half-title h1 now use var(--font-heading); .prompt-code uses var(--font-prompt). Adjusted h1 letter-spacing (-0.01em-0.03em). Changed let rendered to const rendered and updated nearby comments around generateHtmlDocument().

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐇 I hopped through locales, found the right fonts to wear,
East and west, serif, sans — styled with care,
Headings now sing in fitting tone,
Prompts and titles feel at home,
My paws left prints across each page. ✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main fix: preventing missing CJK text in generated PDFs, which directly addresses the root cause and primary change documented in the PR.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (1)
scripts/generate-book-pdf.ts (1)

2208-2211: Consider deriving CJK locales from LOCALE_FONT_STACKS to avoid sync issues.

The isCjk check duplicates knowledge of which locales are CJK (already defined as keys in LOCALE_FONT_STACKS). If a new CJK locale is added to LOCALE_FONT_STACKS but not here, the font selection logic would silently use incorrect heading/prompt fonts.

♻️ Optional: Derive isCjk from LOCALE_FONT_STACKS
+const CJK_LOCALES = Object.keys(LOCALE_FONT_STACKS);
+
 function generateHtmlDocument(chapters: ProcessedChapter[], locale: string, messages: Record<string, unknown> = {}): string {
   // ...
-  const isCjk = ['zh', 'ja', 'ko'].includes(locale);
+  const isCjk = CJK_LOCALES.includes(locale);

Alternatively, keep the explicit array but add a comment linking the two locations.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@scripts/generate-book-pdf.ts` around lines 2208 - 2211, The CJK determination
duplicates locale knowledge; change the isCjk logic in generate-book-pdf.ts to
derive from LOCALE_FONT_STACKS instead of hardcoding ['zh','ja','ko']: use
LOCALE_FONT_STACKS (via its keys or a helper) to decide CJK based on the current
locale, so getFontStacks(locale) and the headingFont/promptFont assignments
(headingFont, promptFont, locale, getFontStacks) stay in sync; alternatively, if
you prefer to keep the explicit list, add a clear comment referencing
LOCALE_FONT_STACKS to avoid future drift.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@scripts/generate-book-pdf.ts`:
- Around line 2208-2211: The CJK determination duplicates locale knowledge;
change the isCjk logic in generate-book-pdf.ts to derive from LOCALE_FONT_STACKS
instead of hardcoding ['zh','ja','ko']: use LOCALE_FONT_STACKS (via its keys or
a helper) to decide CJK based on the current locale, so getFontStacks(locale)
and the headingFont/promptFont assignments (headingFont, promptFont, locale,
getFontStacks) stay in sync; alternatively, if you prefer to keep the explicit
list, add a clear comment referencing LOCALE_FONT_STACKS to avoid future drift.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9108e5f6-edbd-4c08-b98a-f02c0d524c7f

📥 Commits

Reviewing files that changed from the base of the PR and between c148eca and 6d6e335.

📒 Files selected for processing (1)
  • scripts/generate-book-pdf.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant