Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/_data/pdfjsAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
Expand Down
39 changes: 35 additions & 4 deletions src/pdfjs-viewer/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: An individual resource attribute overrides only its own resource; it does not make the plugin copy unused. Clarify that only assets-base bypasses the entire plugin copy.

Prompt for AI agents
Check if this issue is valid β€” if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/pdfjs-viewer/index.md, line 250:

<comment>An individual resource attribute overrides only its own resource; it does not make the plugin copy unused. Clarify that only `assets-base` bypasses the entire plugin copy.</comment>

<file context>
@@ -222,9 +222,38 @@ They don't copy the PDF.js [data folders](#cmaps-fonts-and-other-resources), tho
+
+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.
</file context>
Suggested change
With `assets-base` or a resource attribute set, the element loads the data from there, and the plugin's copy goes unused.
With `assets-base` set, the element loads the data from there, and the plugin's copy goes unused. Individual resource attributes override only their corresponding resource.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #109.


[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`

<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:

```js
// scripts/copy-pdfjs-viewer.mjs
Expand Down Expand Up @@ -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
<pdfjs-viewer-element
Expand All @@ -325,11 +354,12 @@ If your page has a Content Security Policy, the WASM decoders and the scripting
## Frameworks
The element works anywhere HTML does. Three things to remember in every framework:
The element works anywhere HTML does. Four things to remember in every framework:
- 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.
- Give the element a height.
- Attributes are strings. Call methods (`setViewerOptions`, `injectViewerStyles`) through a ref.
- 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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: This summary covers only some Vite-based frameworks. Say that any Vite app should add the plugin, so React or Vue users on plain Vite don't skip it.

Prompt for AI agents
Check if this issue is valid β€” if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/pdfjs-viewer/index.md, line 362:

<comment>This summary covers only some Vite-based frameworks. Say that any Vite app should add the plugin, so React or Vue users on plain Vite don't skip it.</comment>

<file context>
@@ -325,11 +354,12 @@ If your page has a Content Security Policy, the WASM decoders and the scripting
 - 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.
 - Give the element a height.
 - Attributes are strings. Call methods (`setViewerOptions`, `injectViewerStyles`) through a ref.
+- 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.
 
 ### React
</file context>
Suggested change
- 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.
- Bundlers don't copy the data some PDFs need. In a Vite app, add the [Vite plugin](#vite-plugin), or [copy `dist` and set `assets-base`](#other-bundlers-and-assets-base) in Next.js and other bundlers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #109.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P3: The framework checklist omits React Router even though this page lists it as a supported Vite-plugin framework. Include React Router so its production builds do not miss PDF.js data.

Prompt for AI agents
Check if this issue is valid β€” if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/pdfjs-viewer/index.md, line 362:

<comment>The framework checklist omits React Router even though this page lists it as a supported Vite-plugin framework. Include React Router so its production builds do not miss PDF.js data.</comment>

<file context>
@@ -325,11 +354,12 @@ If your page has a Content Security Policy, the WASM decoders and the scripting
 - 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.
 - Give the element a height.
 - Attributes are strings. Call methods (`setViewerOptions`, `injectViewerStyles`) through a ref.
+- 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.
 
 ### React
</file context>
Suggested change
- 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.
- Bundlers don't copy the data some PDFs need. Add the [Vite plugin](#vite-plugin) in React Router, SvelteKit, Nuxt and Astro, or [copy `dist` and set `assets-base`](#other-bundlers-and-assets-base) in Next.js.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #109.

### React
Expand Down Expand Up @@ -505,6 +535,7 @@ import '@missing-elements/pdfjs-viewer' // was: import 'pdfjs-viewer-element'
If you're coming from an older version:
- **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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2: This step doesn't say to remove assets-base. As written, keeping assets-base makes the plugin's copy unused, so the migration gives no benefit. Add that assets-base (and any resource attributes pointing at the copy) must be removed.

Prompt for AI agents
Check if this issue is valid β€” if so, understand the root cause and fix it. When an issue isn't valid or won't be fixed in this PR, reply in its thread with the reason and then resolve the thread. At src/pdfjs-viewer/index.md, line 538:

<comment>This step doesn't say to remove `assets-base`. As written, keeping `assets-base` makes the plugin's copy unused, so the migration gives no benefit. Add that `assets-base` (and any resource attributes pointing at the copy) must be removed.</comment>

<file context>
@@ -505,6 +535,7 @@ import '@missing-elements/pdfjs-viewer' // was: import 'pdfjs-viewer-element'
 
 If you're coming from an older version:
 
+- **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.
 - **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).
 - **From 4.4.0 or earlier:** if your Content Security Policy allows `blob:` in `connect-src` only for locales, you can remove it.
</file context>
Suggested change
- **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.
- **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`. Remove the `assets-base` attribute, because it makes the plugin's copy unused.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #109.

- **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).
- **From 4.4.0 or earlier:** if your Content Security Policy allows `blob:` in `connect-src` only for locales, you can remove it.
- **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).
Expand Down