diff --git a/.gitignore b/.gitignore index 2e5cbea..9b4c848 100644 --- a/.gitignore +++ b/.gitignore @@ -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 dist-ssr *.local diff --git a/README.md b/README.md index 1b4d145..463e6a8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 @@ -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) diff --git a/demo/assets-base.html b/demo/assets-base.html index eebba93..23e0492 100644 --- a/demo/assets-base.html +++ b/demo/assets-base.html @@ -15,11 +15,11 @@ ` 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. diff --git a/demo/vite-plugin/index.html b/demo/vite-plugin/index.html new file mode 100644 index 0000000..41f265a --- /dev/null +++ b/demo/vite-plugin/index.html @@ -0,0 +1,32 @@ + + + + + + pdfjs-viewer | Vite plugin demo + + + +

The page holds one scanned image, compressed with CCITT. Its left half is black. Built without the plugin, the page stays white.

+ + + + diff --git a/demo/vite-plugin/main.js b/demo/vite-plugin/main.js new file mode 100644 index 0000000..643b96d --- /dev/null +++ b/demo/vite-plugin/main.js @@ -0,0 +1 @@ +import '@missing-elements/pdfjs-viewer' diff --git a/demo/vite-plugin/package.json b/demo/vite-plugin/package.json new file mode 100644 index 0000000..15c0ce2 --- /dev/null +++ b/demo/vite-plugin/package.json @@ -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" + } +} diff --git a/demo/vite-plugin/public/scanned.pdf b/demo/vite-plugin/public/scanned.pdf new file mode 100644 index 0000000..6044c90 Binary files /dev/null and b/demo/vite-plugin/public/scanned.pdf differ diff --git a/demo/vite-plugin/vite.config.js b/demo/vite-plugin/vite.config.js new file mode 100644 index 0000000..f5c312f --- /dev/null +++ b/demo/vite-plugin/vite.config.js @@ -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()] +}