Skip to content

fix(web): pass pdf.js cMap/standard font URLs so CID-font PDFs render in preview - #18249

Merged
wangq8 merged 1 commit into
infiniflow:mainfrom
euvre:fix/web-pdf-cid-font-cmaps
Aug 14, 2026
Merged

wangq8 merged 1 commit into
infiniflow:mainfrom
euvre:fix/web-pdf-cid-font-cmaps

Conversation

@euvre

@euvre euvre commented Aug 13, 2026

Copy link
Copy Markdown
Contributor

What

Knowledge-base PDF preview renders blank pages for PDFs that use built-in CID fonts (very common for CJK documents, e.g. the Tencent PDF in #8813). The canvas paints nothing and the text layer stays empty.

Root cause

PdfLoader (react-pdf-highlighter / pdf.js) was used without cMapUrl / cMapPacked / standardFontDataUrl. pdf.js therefore could not load the required CMaps (UniGB-UCS2-H, etc.) and standard font data, failing with console warnings:

Warning: CMapReaderFactory not initialized, using default CMapReaderFactory

For CID-keyed fonts this means glyphs cannot be resolved, so pages render empty even though the document parses fine.

Fix

  • web/vite.config.ts: copy pdfjs-dist/cmaps/ and pdfjs-dist/standard_fonts/ into the already-served public/pdfjs-dist/ directory via the existing viteStaticCopy setup (same mechanism already used for pdf.worker.min.js).
  • web/src/components/document-preview/pdf-preview.tsx: pass cMapUrl="/pdfjs-dist/cmaps/", cMapPacked={true} and standardFontDataUrl="/pdfjs-dist/standard_fonts/" to PdfLoader (and extend the local PdfLoaderProps type with standardFontDataUrl, which react-pdf-highlighter's typings don't expose).

12 lines added, 0 removed.

Verification (dev server, knowledge-base document preview)

Reproduced with a CJK CID-font PDF (\u56fd\u5bcc\u8bba / Wealth of Nations, Chinese) uploaded to a knowledge base:

Before (props removed) After (this PR)
textLayer text length 0 full Chinese text of the page
canvas 2 blank canvases 2 canvases with glyphs painted
console CMapReaderFactory not initialized warnings no pdf.js warnings/errors

With the fix, the text layer contains the complete selectable Chinese text (e.g. \u7b2c\u4e00\u7bc7 \u8bba\u52b3\u52a8\u751f\u4ea7\u529b\u589e\u8fdb\u7684\u539f\u56e0...) and the page visually renders all characters; both cmaps/ and standard_fonts/ are served with HTTP 200 under /pdfjs-dist/.

Notes

… in preview

PdfLoader was used without cMapUrl/cMapPacked/standardFontDataUrl, so
pdf.js could not load the UniGB-UCS2-H etc. CMaps and PDFs using
built-in CID fonts (common for CJK documents) rendered blank pages
with 'CMapReaderFactory not initialized' errors.

Copy pdfjs-dist cmaps/ and standard_fonts/ into the served pdfjs-dist/
directory via viteStaticCopy and point PdfLoader at them.
@dosubot dosubot Bot added size:S This PR changes 10-29 lines, ignoring generated files. 🐞 bug Something isn't working, pull request that fix bug. 🧰 typescript Pull requests that update Typescript code labels Aug 13, 2026
@euvre euvre added the ci Continue Integration label Aug 13, 2026
@euvre
euvre requested a review from wangq8 August 13, 2026 19:12
@coderabbitai

coderabbitai Bot commented Aug 13, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: a871be86-4155-4274-bcca-c9c13e3d7265

📥 Commits

Reviewing files that changed from the base of the PR and between 423c848 and 1a8beb4.

📒 Files selected for processing (2)
  • web/src/components/document-preview/pdf-preview.tsx
  • web/vite.config.ts

📝 Walkthrough

Walkthrough

The PDF preview configures PDF.js to load packed character maps and standard fonts. Vite copies both asset directories into the pdfjs-dist/ build output.

Changes

PDF.js asset loading

Layer / File(s) Summary
Configure PDF.js loader assets
web/src/components/document-preview/pdf-preview.tsx, web/vite.config.ts
PdfLoaderProps supports standardFontDataUrl. Loader uses packed character maps and standard fonts. Vite copies both directories under pdfjs-dist/.

Estimated code review effort: 1 (Trivial) | ~5 minutes

Mergeability Score: ⚪ Minimal · up to 1a8be

This localized change supplies the PDF resources needed for CID-font documents to render correctly in preview; no actionable merge-blocking risk remains beyond normal checks and review.

Suggested reviewers: cike8899, jinhai-cn, xugangqiang

Poem

I’m a rabbit with fonts in my pack,
PDF.js maps on the track.
Vite copies each file,
The loader hops in style,
And clear pages spring back.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely identifies the PDF.js CMap and standard font URL fix for CID-font PDF rendering.
Description check ✅ Passed The description clearly explains the issue, root cause, fix, verification, and testing results, although it does not use the template's Summary heading.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

@wangq8 wangq8 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Reviewed. The fix is correct: CID-keyed PDFs need cMapUrl/cMapPacked and standardFontDataUrl to resolve glyphs. Confirmed:

  • react-pdf-highlighter's PdfLoader spreads all props into getDocument (Object.assign({}, this.props, ...)), so standardFontDataUrl is forwarded despite the missing typing; extending PdfLoaderProps is the right fix.
  • viteStaticCopy copies the source directory name into dest (same pattern as monaco-editor/min/vs → /vs), so the targets produce public/pdfjs-dist/cmaps/ and public/pdfjs-dist/standard_fonts/, matching the URLs.

Two minor, non-blocking notes:

  1. The description says this uses the "same mechanism already used for pdf.worker.min.js" — actually pdf.worker.min.js is checked in under web/public/pdfjs-dist/, not copied by viteStaticCopy. The mechanism used here (viteStaticCopy) is correct and already proven for monaco, just not for the worker.
  2. Copying the full cmaps/ directory adds non-trivial static assets to the build output; with cMapPacked=true only the .bcmap files are needed, so the copy could potentially be trimmed (optional).
    LGTM.

@wangq8
wangq8 requested a review from dcc123456 August 14, 2026 07:57
@wangq8
wangq8 merged commit ea3a2b2 into infiniflow:main Aug 14, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🐞 bug Something isn't working, pull request that fix bug. ci Continue Integration size:S This PR changes 10-29 lines, ignoring generated files. 🧰 typescript Pull requests that update Typescript code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants