From 96986209678e50a8007c0b55b6b59b9214fa032c Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Tue, 24 Nov 2026 21:15:39 +0200 Subject: [PATCH 1/2] docs: Vite plugin demo app and README for 4.6.0 demo/vite-plugin is a minimal Vite app that installs the published package and uses the plugin; its PDF holds one CCITT image, which stays white when the app is built without the plugin. The README names the Vite-based frameworks, links the docs' new Vite plugin and other-bundlers sections, and points to the caching note before an upgrade. .gitignore now ignores only the root public/ folder, so the demo can keep its PDF in its own public/. Co-Authored-By: Claude Opus 5.5 --- .gitignore | 5 ++++- README.md | 8 ++++---- demo/assets-base.html | 10 +++++----- demo/vite-plugin/README.md | 17 +++++++++++++++++ demo/vite-plugin/index.html | 27 +++++++++++++++++++++++++++ demo/vite-plugin/main.js | 1 + demo/vite-plugin/package.json | 16 ++++++++++++++++ demo/vite-plugin/public/scanned.pdf | Bin 0 -> 1226 bytes demo/vite-plugin/vite.config.js | 7 +++++++ 9 files changed, 81 insertions(+), 10 deletions(-) create mode 100644 demo/vite-plugin/README.md create mode 100644 demo/vite-plugin/index.html create mode 100644 demo/vite-plugin/main.js create mode 100644 demo/vite-plugin/package.json create mode 100644 demo/vite-plugin/public/scanned.pdf create mode 100644 demo/vite-plugin/vite.config.js 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..a51ce2d 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, read the [Vite plugin](https://missing-elements.github.io/pdfjs-viewer/#vite-plugin) docs before you upgrade. [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..f6e4697 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..4e136b9 --- /dev/null +++ b/demo/vite-plugin/index.html @@ -0,0 +1,27 @@ + + + + + + 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 0000000000000000000000000000000000000000..6044c904a1da25e2ce69cb7409db7ae1cc44971f GIT binary patch literal 1226 zcmZWp?{C^L5dEIN;(bBdBuWy>uT>RN3!`J*)UKdzQne2bIlwfrnQaR6uix2$wOtL7 zo%7whchBe4Zil0jw9$%cxWHO02yM`Jx3R%4;=DNtqfL%*ztyGH!rcARcBo|K9 zkxA!kF7fRusWjr)W-8zMMalC89~q9h1gNywx`rk^N)5vx><UP3A)Nrd(4D**=t7&xTCj%kke*n}D$I;g6^0E2>_0QIB`fKx_t=oHN u>n87QpE|GboDs4h6Iyp7pNYQ~tdsU#cW7C@`o;yGY0_#&wc6*KQS=u#&022& literal 0 HcmV?d00001 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()] +} From 35750b465eb9386ea581f34791eb2778ba732085 Mon Sep 17 00:00:00 2001 From: Aleksandr Shevchuk Date: Tue, 24 Nov 2026 21:21:26 +0200 Subject: [PATCH 2/2] docs: fix the #85 review notes The README says what to do about an immutable cache instead of only pointing to the docs. The assets-base demo comment no longer reads as if the demo loads from a CDN. The Vite plugin demo lays the page out as a flex column, so the viewer fills what the text leaves on any width instead of a fixed calc(). Co-Authored-By: Claude Opus 5.5 --- README.md | 2 +- demo/assets-base.html | 4 ++-- demo/vite-plugin/index.html | 7 ++++++- 3 files changed, 9 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index a51ce2d..463e6a8 100644 --- a/README.md +++ b/README.md @@ -39,7 +39,7 @@ export default { } ``` -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, read the [Vite plugin](https://missing-elements.github.io/pdfjs-viewer/#vite-plugin) docs before you upgrade. [demo/vite-plugin](./demo/vite-plugin) is a minimal app that uses the plugin. +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. **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: diff --git a/demo/assets-base.html b/demo/assets-base.html index f6e4697..23e0492 100644 --- a/demo/assets-base.html +++ b/demo/assets-base.html @@ -18,8 +18,8 @@ `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 also - loads everything from a CDN. Here it simply points back at `dist` for demonstration. + 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 | Vite plugin demo