You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 4e5608b
Browse filesBrowse the repository at this point in the historyBrowse files
Bump pdfjs-viewer to 4.6.0 and document the Vite plugin
Hosting splits into Bundlers, Vite plugin, and Other bundlers and
`assets-base`. The old #bundlers-and-assets-base anchor still lands on
the copy route. The Vite plugin section says where the data goes, which
frameworks take it, and that its fixed file names can outlive an
upgrade in an immutable cache.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copy file name to clipboardExpand all lines: src/_data/pdfjsAttributes.js
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -89,7 +89,7 @@ export default [
89
89
{
90
90
name: "assets-base",
91
91
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).",
Copy file name to clipboardExpand all lines: src/pdfjs-viewer/index.md
+35-4Lines changed: 35 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -211,7 +211,7 @@ If your page has a Content Security Policy, allow the locale file's origin in `c
211
211
212
212
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.
213
213
214
-
### Bundlers and `assets-base`
214
+
### Bundlers
215
215
216
216
**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.
217
217
@@ -222,9 +222,38 @@ They don't copy the PDF.js [data folders](#cmaps-fonts-and-other-resources), tho
222
222
- JavaScript in PDFs, such as form calculations, doesn't run.
223
223
- Note icons are missing, and ICC color profiles aren't applied.
224
224
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
+
225
227
**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.
226
228
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:
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
+
<spanid="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:
228
257
229
258
```js
230
259
// 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
301
330
302
331
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.
303
332
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:
305
334
306
335
```html
307
336
<pdfjs-viewer-element
@@ -325,11 +354,12 @@ If your page has a Content Security Policy, the WASM decoders and the scripting
325
354
326
355
## Frameworks
327
356
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:
329
358
330
359
- 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.
331
360
- Give the element a height.
332
361
- 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.
- **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.
508
539
- **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).
509
540
- **From 4.4.0 or earlier:** if your Content Security Policy allows `blob:` in `connect-src` only for locales, you can remove it.
510
541
- **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