Repository navigation
fix: resource defaults and URLs, region locales, navigation state, viewer options, forced colors - #63
Merged
Merged
Conversation
PDF.js fetches CMaps, standard fonts, WASM decoders and ICC profiles from
its worker, which runs from a blob: URL, so a relative c-map-url,
standard-font-data-url, wasm-url or icc-url never loaded. The element now
resolves these attributes, plus sandbox-bundle-src and debugger-src,
against the page the same way it resolves src and the runtime files, and
adds the trailing slash PDF.js requires on folder URLs ("Invalid factory
url ... must include trailing slash"). Unset attributes keep their
defaults.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The locale check compared the attribute with the lowercase keys of
locale.json, so locale="pt-BR" was dropped without a message. A lowercase
"pt-br" passed, but went into the template as-is, and the locale folders
keep their case (pt-BR/viewer.ftl), so the file 404ed. The element now
matches the locale ignoring case, fills {locale} with the folder name from
locale.json, and keys the resource by the lowercase code PDF.js looks up.
The filled template is also resolved against the page. PDF.js resolves it
against the blob: URL of the l10n resource link, so a relative template
failed and the viewer showed no interface text at all.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The iframe hash listed every navigation parameter, set or not. PDF.js reads an empty page= as page 1 and the default pagemode=none closes the sidebar, so changing zoom, search, phrase or locale on a viewer without a page attribute sent the reader back to page 1 and closed their sidebar. Unset parameters are now left out, and pagemode falls back to none only when the viewer is built, so the sidebar still starts closed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
debugger-src set PDF.js's debuggerSrc option, but PDF.js only loads its debugger when pdfBugEnabled is set and the viewer hash has a pdfbug parameter, which the element never writes, and debugger.mjs isn't in dist. The attribute never had an effect, so it's removed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…files The defaults for c-map-url, icc-url, standard-font-data-url, wasm-url and sandbox-bundle-src were paths in PDF.js's source tree, for folders the package didn't ship, and image-resources-path defaulted to ./images/ next to the host page. CJK text without embedded fonts, non-embedded standard fonts, JPEG 2000 and JBIG2 images, ICC colors, PDF JavaScript and annotation icons failed unless every attribute was set. dist now includes cmaps/, iccs/, standard_fonts/, wasm/ and pdf.sandbox.mjs. The packed tarball grows from about 0.7 to 2.7 MB; the files load only for documents that need them. Each resource defaults to its folder next to viewer.mjs, and the sandbox to pdf.sandbox.mjs next to pdf.mjs, so CDN script tags, assets-base and the individual runtime URLs carry them along. Bundlers emit the runtime files but not folders, so bundled apps still set assets-base or the attributes. image-resources-path is now resolved and gets a trailing slash like the other folder attributes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PDF.js copies imageResourcesPath into its page viewer when the viewer is created, so changing the attribute later only updated an option that nothing read again. The element now updates that copy too, so the next document uses the new path, like the other resource attributes. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Options were applied once and then cleared, so a locale change that rebuilt the viewer dropped them. They are now kept and applied on every build, and later calls merge with earlier ones instead of replacing them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
viewer.css switches its colors to system colors in forced colors mode, and the browser keeps author system colors while replacing fixed ones. Paper & Ink loads after viewer.css with fixed colors, so its values won, the browser replaced them, and every toolbar icon (a mask filled with a background color) disappeared. The theme now sits in a not (forced-colors: active) block and steps aside in that mode, so PDF.js's own high-contrast styles apply. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… viewer CSP The viewer document's CSP listed only the runtime file origins, so a sandbox-bundle-src or wasm-url on another origin was blocked by script-src (the sandbox is a dynamic import, and so is the QuickJS module it loads from the WASM folder), and annotation icons from an image-resources-path on another origin were blocked by img-src. Those origins are now added, read once per build like the runtime files. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
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.
Fixes the component bugs the docs site currently has to warn about or work around. There's one commit per fix, and each passes the suite on its own.
Fixes
c-map-url,standard-font-data-url,wasm-urlandicc-urlwent to PDF.js as written. PDF.js fetches these folders from its worker, which runs from ablob:URL, so relative paths never resolved, and a folder URL without a trailing/stopped the document from loading. They're now resolved against the page and get the trailing slash.locale="pt-BR"was silently dropped because the keys inlocale.jsonare lowercase.pt-brpassed that check, but the template then gotpt-brwhile the folders are namedpt-BR/. A relativelocale-src-templateblanked every toolbar label. Locales now match ignoring case,{locale}gets the folder name, and the template is resolved against the page.zoom,searchorlocalesent the reader back to page 1 and closed their sidebar. Unset parameters are now left out, andpagemode=noneapplies only when the viewer is built.debugger-srcremoved (33615e9). PDF.js only loads its debugger whenpdfBugEnabledis set and the hash has apdfbugparameter. The element never writes that parameter, anddebugger.mjsisn't indist, so the attribute never did anything.image-resources-pathdefaulted to./images/next to the host page.distnow shipscmaps/,iccs/,standard_fonts/,wasm/andpdf.sandbox.mjs. Each default sits next toviewer.mjs(the sandbox next topdf.mjs), so CDN script tags,assets-baseand custom runtime URLs bring them along.image-resources-pathis resolved and gets its slash like the others.image-resources-pathafter the viewer is built (645023b). PDF.js copies this option into its page viewer once, at creation, so later changes were ignored. The element now updates that copy, and the next document uses the new path.setViewerOptionsacross rebuilds (203dfca). Options were applied once and then cleared, so a locale change that rebuilt the viewer dropped them. They're now kept for every build, and later calls merge with earlier ones.viewer.cssswitches to in forced colors mode. The browser then repainted them, which hid every toolbar icon. The theme now sits in anot (forced-colors: active)block, so PDF.js's own high-contrast styles apply. Custom themes loaded withtheme-css-srcare unaffected.sandbox-bundle-srcorwasm-urlon another origin was blocked byscript-src, and annotation icons from a cross-originimage-resources-pathbyimg-src. Those origins are now included. Found while reviewing this PR; before it, the sandbox never loaded at all, so it didn't show.Behavior changes
enableScriptingis on by default in PDF.js. Before, the sandbox never loaded, so scripts silently didn't run. UsesetViewerOptions({ enableScripting: false })to opt out.{locale}in a customlocale-src-templateis now the folder name with its original case (pt-BR). Copies ofpdfjs-dist/web/locale/and the default CDN work as they are. A copy renamed to lowercase folders (the workaround the docs currently suggest) needs its original names back on case-sensitive hosts.sandbox-bundle-src,wasm-urlorimage-resources-pathto a new origin after the viewer is built isn't covered; set them before connection or rebuild withlocale.#pagemode=none, not#page=&zoom=&pagemode=none&search=&phrase=&locale=.setViewerOptionsmerges with earlier calls instead of replacing them.debugger-srcis gone from observed attributes. README, ACCESSIBILITY.md anddemo/asset-attributes.htmlare updated; the demo no longer needsimage-resources-path.Not covered
Bundled apps (Vite, webpack) still need
assets-baseor the individual attributes for the resource folders. Bundlers emit the runtime files referenced withnew URL('./file', import.meta.url), but they can't emit folders.Verification
pnpm test -- --run: 29 passed. The new or changed tests cover resource URL resolution, default resources reachable next to the runtime files,assets-base,pt-BRfrom a relative template (checks the translated label), the page surviving azoomchange, the liveimage-resources-path, options surviving a rebuild, the forced-colors gate, and the cross-origin CSP entries. Each one fails onmaster, and the suite passes at every commit.tscandpnpm buildare clean.dist, served statically and checked in Chromium against 4.3.0:c-map-url="/pdfjs/cmaps"/pdfjs/cmaps/dist/cmaps/locale="pt-BR", relative templatelocale="zh-CN", default CDN templatezoomsearchpage="9"andpagemode="none"still applyapp.alertOpenAction/images/dist/images/image-resources-pathset after build, new documentAfter release
The docs site can drop its "must be absolute", trailing-slash, region-locale and navigation-reset notes, and stop telling people to copy the resource folders unless they use a bundler.
🤖 Generated with Claude Code