Skip to content

Commit 87b84aa

Browse files
authored
Merge pull request #108 from missing-elements/docs/pdfjs-viewer-4.6.0
Update pdfjs-viewer docs for v4.6.0: the Vite plugin
2 parents 9771190 + 4e5608b commit 87b84aa

4 files changed

Lines changed: 42 additions & 11 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"@missing-elements/h5p-libraries": "1.1.1",
3333
"@missing-elements/h5p-offline-player": "1.7.0",
3434
"@missing-elements/h5p-runtime": "1.0.2",
35-
"@missing-elements/pdfjs-viewer": "4.5.0",
35+
"@missing-elements/pdfjs-viewer": "4.6.0",
3636
"@zip.js/zip.js": "2.25.0"
3737
}
3838
}

‎pnpm-lock.yaml‎

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/_data/pdfjsAttributes.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ export default [
8989
{
9090
name: "assets-base",
9191
group: "files",
92-
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).",
92+
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).",
9393
default: "Folder of `pdfjs-viewer-element.js`",
9494
update: "init",
9595
},

‎src/pdfjs-viewer/index.md‎

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ If your page has a Content Security Policy, allow the locale file's origin in `c
211211

212212
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.
213213

214-
### Bundlers and `assets-base`
214+
### Bundlers
215215

216216
**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.
217217

@@ -222,9 +222,38 @@ They don't copy the PDF.js [data folders](#cmaps-fonts-and-other-resources), tho
222222
- JavaScript in PDFs, such as form calculations, doesn't run.
223223
- Note icons are missing, and ICC color profiles aren't applied.
224224

225+
`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.
226+
225227
**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.
226228

227-
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:
229+
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).
230+
231+
### Vite plugin
232+
233+
Since version 4.6 the package ships a Vite plugin that copies the data into the build output:
234+
235+
```js
236+
// vite.config.js
237+
import { pdfjsViewerData } from '@missing-elements/pdfjs-viewer/vite'
238+
239+
export default {
240+
plugins: [pdfjsViewerData()]
241+
}
242+
```
243+
244+
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.
245+
246+
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`.
247+
248+
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.
249+
250+
With `assets-base` or a resource attribute set, the element loads the data from there, and the plugin's copy goes unused.
251+
252+
[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.
253+
254+
### Other bundlers and `assets-base`
255+
256+
<span id="bundlers-and-assets-base"></span>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:
228257

229258
```js
230259
// 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
301330
302331
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.
303332
304-
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:
333+
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:
305334
306335
```html
307336
<pdfjs-viewer-element
@@ -325,11 +354,12 @@ If your page has a Content Security Policy, the WASM decoders and the scripting
325354
326355
## Frameworks
327356
328-
The element works anywhere HTML does. Three things to remember in every framework:
357+
The element works anywhere HTML does. Four things to remember in every framework:
329358
330359
- Import the package once, on the client. It needs `window`, so in server-rendered apps (Next.js, Nuxt, SvelteKit, Astro) import it in a client-only hook or component.
331360
- Give the element a height.
332361
- Attributes are strings. Call methods (`setViewerOptions`, `injectViewerStyles`) through a ref.
362+
- Bundlers don't copy the data some PDFs need. Add the [Vite plugin](#vite-plugin) in SvelteKit, Nuxt and Astro, or [copy `dist` and set `assets-base`](#other-bundlers-and-assets-base) in Next.js.
333363
334364
### React
335365
@@ -505,6 +535,7 @@ import '@missing-elements/pdfjs-viewer' // was: import 'pdfjs-viewer-element'
505535
506536
If you're coming from an older version:
507537
538+
- **From 4.5.0 or earlier:** nothing needs to change. In a Vite app, the new [Vite plugin](#vite-plugin) can replace a copy of `dist` that you serve with `assets-base` for the data.
508539
- **From 4.4.2 or earlier:** version 4.5 moved from PDF.js 6.4 to 6.5. If you set `worker-src`, point it at `pdfjs-dist@{{ pdfjsViewer.pdfjsVersion }}`. TypeScript now rejects a bare `Uint8Array` in `viewerApp.open()`, which already failed at runtime. Pass `{ data }`, as in [Open a PDF from bytes](#open-a-pdf-from-bytes).
509540
- **From 4.4.0 or earlier:** if your Content Security Policy allows `blob:` in `connect-src` only for locales, you can remove it.
510541
- **From 4.3 or earlier:** if you worked around region locales with a lowercase code and lowercase locale folders, rename the folders back to their original case, such as `pt-BR`, and use any case in `locale`. PDF JavaScript now runs; see [PDF.js resources](#cmaps-fonts-and-other-resources) to turn it off. Absolute resource URLs keep working, and `debugger-src` is gone (it never had an effect).

0 commit comments

Comments
 (0)