diff --git a/package.json b/package.json index 77c6dd4..d6762a5 100644 --- a/package.json +++ b/package.json @@ -32,7 +32,7 @@ "@missing-elements/h5p-libraries": "1.1.1", "@missing-elements/h5p-offline-player": "1.7.0", "@missing-elements/h5p-runtime": "1.0.2", - "@missing-elements/pdfjs-viewer": "4.5.0", + "@missing-elements/pdfjs-viewer": "4.6.0", "@zip.js/zip.js": "2.25.0" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 869f87b..5246887 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -45,8 +45,8 @@ importers: specifier: 1.0.2 version: 1.0.2 '@missing-elements/pdfjs-viewer': - specifier: 4.5.0 - version: 4.5.0 + specifier: 4.6.0 + version: 4.6.0 '@zip.js/zip.js': specifier: 2.25.0 version: 2.25.0 @@ -191,8 +191,8 @@ packages: resolution: {integrity: sha512-Z97zWfBZW2jRQQoacGZmyO0coMHM8KlbjK/UAXZFVFvqSuV3lznuNONFdxz34sXUED2asAVW/tS58rDY23xtqA==} engines: {node: '>=20'} - '@missing-elements/pdfjs-viewer@4.5.0': - resolution: {integrity: sha512-BxwuHJxt0PYGNYweqQzV5XBHxdFmZRrmyrnDyQUmNvUPsdAMja+Ms86Kxz+9ruWrntw1Zg/gX5kr2o/en6wo4Q==} + '@missing-elements/pdfjs-viewer@4.6.0': + resolution: {integrity: sha512-3j2hsG6JDdQCJDq2Clf2tgS5JdHozLolRNlcVgZ3/9iL5W0YeSCFM8855gYuau/IQBQQTMYszLL8BHSr8rJXkQ==} '@napi-rs/canvas-android-arm64@1.0.13': resolution: {integrity: sha512-trn7AaDzQ+yeeA0ezhkC7cB1ZDPtp4UYk0EvB5F8qtYL9jEVWtVrU30WOpI52DszoTxaTsCgf5PP4ZVaOlOIHQ==} @@ -1302,7 +1302,7 @@ snapshots: '@missing-elements/h5p-runtime@1.0.2': {} - '@missing-elements/pdfjs-viewer@4.5.0': + '@missing-elements/pdfjs-viewer@4.6.0': dependencies: pdfjs-dist: 6.5.438 diff --git a/src/_data/pdfjsAttributes.js b/src/_data/pdfjsAttributes.js index 39dc205..14a6fc3 100644 --- a/src/_data/pdfjsAttributes.js +++ b/src/_data/pdfjsAttributes.js @@ -89,7 +89,7 @@ export default [ { name: "assets-base", group: "files", - description: "Folder URL of the package's `dist` files: the runtime files and the PDF.js data folders. Needed when your bundler doesn't emit the files, when a bundled app's documents need the data, or to load everything from a CDN. See [Bundlers](#bundlers-and-assets-base).", + description: "Folder URL of the package's `dist` files: the runtime files and the PDF.js data folders. Needed when your bundler doesn't emit the files, when a bundled app without the [Vite plugin](#vite-plugin) needs the data, or to load everything from a CDN. See [Bundlers](#bundlers).", default: "Folder of `pdfjs-viewer-element.js`", update: "init", }, diff --git a/src/pdfjs-viewer/index.md b/src/pdfjs-viewer/index.md index 5169be9..f041326 100644 --- a/src/pdfjs-viewer/index.md +++ b/src/pdfjs-viewer/index.md @@ -211,7 +211,7 @@ If your page has a Content Security Policy, allow the locale file's origin in `c The element loads the viewer at runtime from five files next to `pdfjs-viewer-element.js` in the package's `dist` folder: `viewer.mjs`, `pdf.mjs`, `pdf.worker.min.mjs`, `viewer.css` and `paper-and-ink.css`. All of them must come from the same release as the element. The [data some PDFs need](#cmaps-fonts-and-other-resources) sits beside them. With [worker rendering](#worker) on, the viewer also loads `pdf.renderer.min.mjs` from the same folder. -### Bundlers and `assets-base` +### Bundlers **Vite, webpack 5 and Next.js**, with webpack or Turbopack, need no configuration for the viewer. The element references its files as `new URL('./file', import.meta.url)`, so these bundlers emit them as assets and rewrite the URLs. @@ -222,9 +222,38 @@ They don't copy the PDF.js [data folders](#cmaps-fonts-and-other-resources), tho - JavaScript in PDFs, such as form calculations, doesn't run. - Note icons are missing, and ICC color profiles aren't applied. +`vite dev` serves the package's own files, so in a Vite app all of this works during development and breaks only in the build. + **esbuild, plain Rollup and other bundlers** that don't handle that pattern move the element into your bundle without the files, and the viewer fails with 404s. -To support every PDF with any bundler, copy the package's `dist` folder into your static files and point `assets-base` at it. A `postinstall` script keeps the copy at the installed version: +To support every PDF, add the [Vite plugin](#vite-plugin) in a Vite app. With any other bundler, [copy `dist` and set `assets-base`](#other-bundlers-and-assets-base). + +### Vite plugin + +Since version 4.6 the package ships a Vite plugin that copies the data into the build output: + +```js +// vite.config.js +import { pdfjsViewerData } from '@missing-elements/pdfjs-viewer/vite' + +export default { + plugins: [pdfjsViewerData()] +} +``` + +It finds `viewer.mjs` and `pdf.mjs` in the build output under whatever names Vite gives them, and writes the data where the element looks for it: `wasm/`, `cmaps/`, `standard_fonts/`, `iccs/` and `images/` next to `viewer.mjs`, and `pdf.sandbox.mjs` next to `pdf.mjs`. That adds 286 files, 3.4 MB, to the build, and browsers download only the files a document needs. The plugin works with Vite 4.5 and later and adds nothing to server builds. It doesn't work with webpack, Rspack, Turbopack or esbuild. + +Frameworks built on Vite take it in their Vite config: `vite.config.js` in SvelteKit and React Router, and `vite.plugins` in `nuxt.config.ts` and `astro.config.mjs`. + +The copied files keep the names PDF.js asks for, so their URLs stay the same across upgrades. SvelteKit, Nuxt and Astro serve the build's asset folder as `immutable`, and many hosts do the same for Vite's `assets/`. A browser that visited before an upgrade can then keep the old files for as long as its cache lasts, often a year. PDF.js can't use data from another PDF.js version: the `openjpeg.wasm` that shipped with 4.4, for example, doesn't load in 4.6. If your host lets you, serve the data folders with a short cache. Otherwise, [copy `dist` and set `assets-base`](#other-bundlers-and-assets-base) instead. + +With `assets-base` or a resource attribute set, the element loads the data from there, and the plugin's copy goes unused. + +[demo/vite-plugin](https://github.com/missing-elements/pdfjs-viewer/tree/master/demo/vite-plugin) in the repository is a minimal app that uses the plugin. + +### Other bundlers and `assets-base` + +With webpack, Next.js and any other bundler, copy the package's `dist` folder into your static files and point `assets-base` at it. This works in Vite too. A `postinstall` script keeps the copy at the installed version: ```js // scripts/copy-pdfjs-viewer.mjs @@ -301,7 +330,7 @@ That worker runs the bundled `pdf.renderer.min.mjs`. PDF.js starts it from a `bl Most PDFs render with just the viewer files. Some need extra PDF.js data: CMaps for some CJK text, standard fonts for non-embedded base fonts, WASM decoders for JBIG2, CCITT and JPEG 2000 images, ICC color profiles, annotation icons, and the scripting sandbox for PDFs with JavaScript. All of it ships in the package and loads only for documents that need it. -Each resource is loaded from next to the viewer files by default, so nothing needs setting when the element comes from a CDN or you serve `dist` with `assets-base`. In bundled apps, [copy `dist` and set `assets-base`](#bundlers-and-assets-base), or point the attributes at your own copy. Relative URLs work, and folder URLs get a missing trailing `/` added: +Each resource is loaded from next to the viewer files by default, so nothing needs setting when the element comes from a CDN or you serve `dist` with `assets-base`. In bundled apps, add the [Vite plugin](#vite-plugin) or [copy `dist` and set `assets-base`](#other-bundlers-and-assets-base), or point the attributes at your own copy. Relative URLs work, and folder URLs get a missing trailing `/` added: ```html