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
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ pnpm-debug.log*
lerna-debug.log*

node_modules
public
# The PDF.js release the sync script downloads. Only the root one: demos keep their own `public`.
/public
dist
# The Vite plugin demo installs the published package; its lockfile would only go stale here.
demo/vite-plugin/package-lock.json
Comment thread
alekswebnet marked this conversation as resolved.
Resolved
dist-ssr
*.local

Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import '@missing-elements/pdfjs-viewer'

Vite, webpack 5 and Next.js bundle the viewer with no configuration. They don't copy the PDF.js data folders, though, because PDF.js loads those files by name. Without them, scanned pages compressed with JBIG2, CCITT or JPEG 2000 render blank or black, CJK text in fonts the PDF doesn't embed goes missing, and JavaScript in PDFs doesn't run.

With Vite, add the plugin that ships with the package. It writes the data into the build output, next to the viewer files: about 3.4 MB, which a browser downloads only for a document that needs it.
**Vite**, and frameworks built on it such as SvelteKit, Nuxt and Astro: add the plugin that ships with the package. It copies the data into the build output, next to the viewer files. That's 3.4 MB, and browsers download only the files a document needs.

```js
// vite.config.js
Expand All @@ -39,9 +39,9 @@ export default {
}
```

The data files keep the names PDF.js asks for, with no hash in them. If your server caches the whole `assets` folder as `immutable`, leave them out of that rule, or browsers keep the old files after you upgrade.
The copied files keep the names PDF.js asks for, so an upgrade doesn't rename them. If your host caches the build's asset folder as `immutable`, as SvelteKit, Nuxt and Astro do, serve the data folders with a short cache, or use `assets-base` below. Otherwise browsers can keep the old files after an upgrade, and PDF.js can't use them. See the [Vite plugin](https://missing-elements.github.io/pdfjs-viewer/#vite-plugin) docs. [demo/vite-plugin](./demo/vite-plugin) is a minimal app that uses the plugin.

With webpack, Next.js or another bundler, copy the package's `dist` folder into your public files, for example in a `postinstall` script, and point [`assets-base`](https://missing-elements.github.io/pdfjs-viewer/#hosting) at it:
**webpack, Next.js and other bundlers:** copy the package's `dist` folder into your public files, for example in a `postinstall` script, and point [`assets-base`](https://missing-elements.github.io/pdfjs-viewer/#other-bundlers-and-assets-base) at it:

```html
<pdfjs-viewer-element src="/file.pdf" assets-base="/pdfjs-viewer/"></pdfjs-viewer-element>
Expand Down Expand Up @@ -72,7 +72,7 @@ The element needs an explicit height. Most attributes apply as soon as they chan
- [Attributes and playground](https://missing-elements.github.io/pdfjs-viewer/#attributes)
- [Methods and properties](https://missing-elements.github.io/pdfjs-viewer/#methods-and-properties): `initPromise`, `setViewerOptions`, `injectViewerStyles`, `iframe`
- [Theming](https://missing-elements.github.io/pdfjs-viewer/#theming) and [localization](https://missing-elements.github.io/pdfjs-viewer/#localization)
- [Hosting](https://missing-elements.github.io/pdfjs-viewer/#hosting): bundlers, `assets-base`, CDN, worker, CSP
- [Hosting](https://missing-elements.github.io/pdfjs-viewer/#hosting): bundlers, the Vite plugin, `assets-base`, CDN, worker, CSP
- [React, Vue and Svelte](https://missing-elements.github.io/pdfjs-viewer/#frameworks)
- [Migrating from `pdfjs-viewer-element`](https://missing-elements.github.io/pdfjs-viewer/#migrating-from-pdfjs-viewer-element), the deprecated former package name
- [Demo pages](./demo) and the [accessibility guide](./ACCESSIBILITY.md)
Expand Down
10 changes: 5 additions & 5 deletions demo/assets-base.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

<body>
<!--
`assets-base` points at a folder holding the package `dist` files
(viewer.mjs, pdf.mjs, pdf.worker.min.mjs, pdf.renderer.min.mjs, viewer.css, paper-and-ink.css).
Bundlers that understand `new URL('./file', import.meta.url)` do not need it; it is the
override for other bundlers or for serving the files from a CDN. Here it simply points
back at `dist` for demonstration.
`assets-base` points at a folder holding the package `dist` files: the runtime files
(viewer.mjs, pdf.mjs, pdf.worker.min.mjs, pdf.renderer.min.mjs, viewer.css, paper-and-ink.css)
and the PDF.js data folders. Bundlers emit the runtime files but not the data, so bundled
apps point it at a copy of `dist`, unless they use the Vite plugin (see vite-plugin/). It can
also point at a CDN. Here it simply points back at `dist` for demonstration.
-->
<pdfjs-viewer-element
src="/public/sample-pdf-with-images.pdf"
Expand Down
17 changes: 17 additions & 0 deletions demo/vite-plugin/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Vite plugin demo

A minimal Vite app with `<pdfjs-viewer-element>` and the package's Vite plugin. Its PDF holds one scanned image compressed with CCITT. PDF.js decodes that with `wasm/jbig2.wasm`, one of the data files the plugin copies into the build.

```bash
npm install
npm run build
npm run preview
```

Open the address `vite preview` prints. The left half of the page is black.

To see the problem the plugin solves, remove `pdfjsViewerData()` from [vite.config.js](vite.config.js) and build again. The page stays white, and the console says `Unable to decode image "img_p0_1": "Jbig2Error: JBig2 failed to initialize"`. The build has no `assets/wasm/` folder: `vite preview` answers those requests with the app's HTML page, and a static host answers 404.

`npm run dev` shows the image with or without the plugin, because the dev server serves the package's own files. That is why the problem only appears after a build.

The demo installs the published package, version 4.6.0 or later.
32 changes: 32 additions & 0 deletions demo/vite-plugin/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>pdfjs-viewer | Vite plugin demo</title>
<style>
/* The viewer fills what the text leaves, however many lines the text wraps to. */
body {
display: flex;
flex-direction: column;
height: 100dvh;
margin: 0;
font-family: system-ui, sans-serif;
}

p {
margin: 1rem;
}

pdfjs-viewer-element {
flex: 1;
min-height: 0;
}
</style>
</head>
<body>
<p>The page holds one scanned image, compressed with CCITT. Its left half is black. Built without the plugin, the page stays white.</p>
<pdfjs-viewer-element src="/scanned.pdf"></pdfjs-viewer-element>
<script type="module" src="./main.js"></script>
</body>
</html>
1 change: 1 addition & 0 deletions demo/vite-plugin/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@missing-elements/pdfjs-viewer'
16 changes: 16 additions & 0 deletions demo/vite-plugin/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "pdfjs-viewer-vite-plugin-demo",
"private": true,
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"@missing-elements/pdfjs-viewer": "^4.6.0"
},
"devDependencies": {
"vite": "^8.3.8"
}
}
Binary file added demo/vite-plugin/public/scanned.pdf
Binary file not shown.
7 changes: 7 additions & 0 deletions demo/vite-plugin/vite.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { pdfjsViewerData } from '@missing-elements/pdfjs-viewer/vite'

// Copies the PDF.js data (WASM decoders, CMaps, fonts, ICC profiles, icons and the scripting
// sandbox) into the build output, next to the viewer files. Remove it to see the problem it solves.
export default {
plugins: [pdfjsViewerData()]
}
Loading