Repository navigation
fix(web): pass pdf.js cMap/standard font URLs so CID-font PDFs render in preview - #18249
Conversation
… 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.
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe PDF preview configures PDF.js to load packed character maps and standard fonts. Vite copies both asset directories into the ChangesPDF.js asset loading
Estimated code review effort: 1 (Trivial) | ~5 minutes Mergeability Score: ⚪ Minimal · up to 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: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
wangq8
left a comment
There was a problem hiding this comment.
Reviewed. The fix is correct: CID-keyed PDFs need cMapUrl/cMapPacked and standardFontDataUrl to resolve glyphs. Confirmed:
- react-pdf-highlighter's
PdfLoaderspreads all props intogetDocument(Object.assign({}, this.props, ...)), sostandardFontDataUrlis forwarded despite the missing typing; extendingPdfLoaderPropsis the right fix. - viteStaticCopy copies the source directory name into dest (same pattern as
monaco-editor/min/vs→/vs), so the targets producepublic/pdfjs-dist/cmaps/andpublic/pdfjs-dist/standard_fonts/, matching the URLs.
Two minor, non-blocking notes:
- The description says this uses the "same mechanism already used for pdf.worker.min.js" — actually
pdf.worker.min.jsis checked in underweb/public/pdfjs-dist/, not copied by viteStaticCopy. The mechanism used here (viteStaticCopy) is correct and already proven for monaco, just not for the worker. - Copying the full
cmaps/directory adds non-trivial static assets to the build output; withcMapPacked=trueonly the.bcmapfiles are needed, so the copy could potentially be trimmed (optional).
LGTM.
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 withoutcMapUrl/cMapPacked/standardFontDataUrl. pdf.js therefore could not load the required CMaps (UniGB-UCS2-H, etc.) and standard font data, failing with console warnings: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: copypdfjs-dist/cmaps/andpdfjs-dist/standard_fonts/into the already-servedpublic/pdfjs-dist/directory via the existingviteStaticCopysetup (same mechanism already used forpdf.worker.min.js).web/src/components/document-preview/pdf-preview.tsx: passcMapUrl="/pdfjs-dist/cmaps/",cMapPacked={true}andstandardFontDataUrl="/pdfjs-dist/standard_fonts/"toPdfLoader(and extend the localPdfLoaderPropstype withstandardFontDataUrl, 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:
0CMapReaderFactory not initializedwarningsWith 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/andstandard_fonts/are served with HTTP 200 under/pdfjs-dist/.Notes
tscerrors, none introduced by this change (the 2TS6133hits inpdf-preview.tsxare pre-existing unused callback params untouched by this diff).oxlinton the touched files: 0 warnings, 0 errors.