Skip to content

Commit 742e7fd

Browse files
authored
Merge pull request #48 from alekswebnet/3.2.0
3.2.0
2 parents 4db6f49 + 0d7920a commit 742e7fd

File tree

6 files changed

+1143
-18
lines changed

6 files changed

+1143
-18
lines changed

README.md

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# pdfjs-viewer-element
22

3-
Custom element that embeds [PDF.js default viewer](https://mozilla.github.io/pdf.js/web/viewer.html) using the `iframe`.
4-
5-
The package provides a custom element, based on PDF.js [viewer options](https://github.com/mozilla/pdf.js/wiki/Viewer-options) and [URL parameters](https://github.com/mozilla/pdf.js/wiki/Debugging-PDF.js#url-parameters) API.
6-
7-
Supported in all [major browsers](https://caniuse.com/custom-elementsv1), and works with most [JS frameworks](https://custom-elements-everywhere.com/).
3+
Standalone, isolated, drop-in [PDF.js default viewer](https://mozilla.github.io/pdf.js/web/viewer.html).
84

95
[![npm version](https://img.shields.io/npm/v/pdfjs-viewer-element?logo=npm&logoColor=fff)](https://www.npmjs.com/package/pdfjs-viewer-element)
106
[![Ask DeepWiki](https://deepwiki.com/badge.svg)](https://deepwiki.com/alekswebnet/pdfjs-viewer-element)
@@ -17,11 +13,12 @@ Supported in all [major browsers](https://caniuse.com/custom-elementsv1), and wo
1713
- Standalone isolated web component with no runtime dependencies
1814
- Drop-in, iframe-based PDF.js default viewer for any web app
1915
- Works with same-origin and cross-origin PDF documents
20-
- Configure via attributes and URL parameters (page, zoom, search, pagemode, locale)
16+
- Configure via attributes (page, zoom, search, pagemode, locale)
2117
- Programmatic access to `PDFViewerApplication` via the `initPromise` public property
22-
- Theme control (automatic/light/dark) plus custom CSS injection
18+
- Built-in Paper & Ink default theme, with theme control (automatic/light/dark) and custom CSS injection
19+
- Resource path attributes for PDF.js internals (`worker-src`, `c-map-url`, `icc-url`, `standard-font-data-url`, `wasm-url`, and more)
2320
- Locale override support using PDF.js viewer locales
24-
- Supports all modern browsers and most JS frameworks
21+
- Supports all [major browsers](https://caniuse.com/custom-elementsv1) and most [JS frameworks](https://custom-elements-everywhere.com/).
2522

2623
## Docs
2724

@@ -89,6 +86,13 @@ The element is block-level and needs an explicit height.
8986
| `locale-src-template` | Locale file URL template. Must contain `{locale}` placeholder. Used together with `locale`. | `https://cdn.jsdelivr.net/gh/mozilla-l10n/firefox-l10n@main/{locale}/toolkit/toolkit/pdfviewer/viewer.ftl` |
9087
| `viewer-css-theme` | Viewer theme: `AUTOMATIC`, `LIGHT`, `DARK`. | `AUTOMATIC` |
9188
| `worker-src` | PDF.js worker URL override. | `<package-url>/pdf.worker.min.mjs` |
89+
| `debugger-src` | PDF.js debugger script URL (`debuggerSrc` option). | `./debugger.mjs` |
90+
| `c-map-url` | CMap directory URL (`cMapUrl` option). | `../web/cmaps/` |
91+
| `icc-url` | ICC profile directory URL (`iccUrl` option). | `../web/iccs/` |
92+
| `image-resources-path` | Image resources directory (`imageResourcesPath` option). | `./images/` |
93+
| `sandbox-bundle-src` | Sandbox bundle URL (`sandboxBundleSrc` option). | `../build/pdf.sandbox.mjs` |
94+
| `standard-font-data-url` | Standard fonts directory (`standardFontDataUrl` option). | `../web/standard_fonts/` |
95+
| `wasm-url` | WASM assets directory (`wasmUrl` option). | `../web/wasm/` |
9296

9397
Play with attributes on [API docs page](https://alekswebnet.github.io/pdfjs-viewer-element/#api).
9498

@@ -99,7 +103,7 @@ Most attributes can be updated dynamically:
99103
- `src` updates by calling PDF.js `open({ url })` without rebuilding the viewer.
100104
- `page`, `search`, `phrase`, `zoom`, `pagemode` update via hash parameters.
101105
- `viewer-css-theme` updates the viewer theme at runtime.
102-
- `worker-src` updates viewer options for subsequent document loads.
106+
- `worker-src`, `debugger-src`, `c-map-url`, `icc-url`, `image-resources-path`, `sandbox-bundle-src`, `standard-font-data-url`, `wasm-url` update viewer options for subsequent document loads.
103107
- `locale` rebuilds the viewer so localization resources can be applied.
104108

105109
## Worker source
@@ -139,6 +143,8 @@ Example:
139143

140144
## Viewer CSS theme
141145

146+
The component includes and applies a default Paper & Ink theme from `src/themes/paper-and-ink.css`.
147+
142148
Use `viewer-css-theme` attribute to set light or dark theme manually:
143149

144150
```html
@@ -157,6 +163,24 @@ viewerElement.setAttribute('viewer-css-theme', 'LIGHT')
157163
viewerElement.setAttribute('viewer-css-theme', 'AUTOMATIC')
158164
```
159165

166+
## PDF.js resource attributes
167+
168+
You can override additional PDF.js viewer resource paths when needed:
169+
170+
```html
171+
<pdfjs-viewer-element
172+
src="/file.pdf"
173+
worker-src="/pdf.worker.min.mjs"
174+
debugger-src="/debugger.mjs"
175+
c-map-url="/cmaps/"
176+
icc-url="/iccs/"
177+
image-resources-path="/images/"
178+
sandbox-bundle-src="/pdf.sandbox.mjs"
179+
standard-font-data-url="/standard_fonts/"
180+
wasm-url="/wasm/">
181+
</pdfjs-viewer-element>
182+
```
183+
160184
## Viewer custom styles
161185

162186
You can add your own CSS rules to the viewer application using `injectViewerStyles(styles: string)`:

image.webp

251 KB
Loading

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pdfjs-viewer-element",
3-
"version": "3.1.2",
3+
"version": "3.2.0",
44
"license": "MIT",
55
"author": {
66
"name": "Oleksandr Shevchuk",

src/pdfjs-viewer-element.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ const DEFAULTS = {
1313
locale: '',
1414
viewerCssTheme: 'AUTOMATIC',
1515
workerSrc: DEFAULT_WORKER_SRC,
16+
debuggerSrc: './debugger.mjs',
17+
cMapUrl: '../web/cmaps/',
18+
iccUrl: '../web/iccs/',
19+
imageResourcesPath: './images/',
20+
sandboxBundleSrc: '../build/pdf.sandbox.mjs',
21+
standardFontDataUrl: '../web/standard_fonts/',
22+
wasmUrl: '../web/wasm/',
1623
localeSrcTemplate: 'https://cdn.jsdelivr.net/gh/mozilla-l10n/firefox-l10n@main/{locale}/toolkit/toolkit/pdfviewer/viewer.ftl'
1724
} as const
1825

@@ -36,7 +43,9 @@ export class PdfjsViewerElement extends HTMLElement {
3643

3744
static get observedAttributes() {
3845
return[
39-
'src', 'locale', 'viewer-css-theme', 'worker-src',
46+
'src', 'locale', 'viewer-css-theme', 'worker-src',
47+
'debugger-src', 'c-map-url', 'icc-url', 'image-resources-path',
48+
'sandbox-bundle-src', 'standard-font-data-url', 'wasm-url',
4049
'page', 'search', 'phrase', 'zoom', 'pagemode', 'iframe-title'
4150
]
4251
}
@@ -182,6 +191,13 @@ export class PdfjsViewerElement extends HTMLElement {
182191
private applyViewerOptions = () => {
183192
const viewerOptions = this.iframe.contentWindow?.PDFViewerApplicationOptions
184193
viewerOptions?.set('workerSrc', this.getAttribute('worker-src') || DEFAULTS.workerSrc)
194+
viewerOptions?.set('debuggerSrc', this.getAttribute('debugger-src') || DEFAULTS.debuggerSrc)
195+
viewerOptions?.set('cMapUrl', this.getAttribute('c-map-url') || DEFAULTS.cMapUrl)
196+
viewerOptions?.set('iccUrl', this.getAttribute('icc-url') || DEFAULTS.iccUrl)
197+
viewerOptions?.set('imageResourcesPath', this.getAttribute('image-resources-path') || DEFAULTS.imageResourcesPath)
198+
viewerOptions?.set('sandboxBundleSrc', this.getAttribute('sandbox-bundle-src') || DEFAULTS.sandboxBundleSrc)
199+
viewerOptions?.set('standardFontDataUrl', this.getAttribute('standard-font-data-url') || DEFAULTS.standardFontDataUrl)
200+
viewerOptions?.set('wasmUrl', this.getAttribute('wasm-url') || DEFAULTS.wasmUrl)
185201
viewerOptions?.set('defaultUrl', this.getFullPath(this.getAttribute('src') || DEFAULTS.src))
186202
viewerOptions?.set('disablePreferences', true)
187203
viewerOptions?.set('eventBusDispatchToDOM', true)
@@ -203,13 +219,15 @@ export class PdfjsViewerElement extends HTMLElement {
203219

204220
private buildViewerEntry = async () => {
205221
return new Promise<void>(async (resolve) => {
206-
const [viewerEntry, viewerCss] = await Promise.all([
222+
const [viewerEntry, viewerCss, paperAndInkTheme] = await Promise.all([
207223
import('./web/viewer.html?raw'),
208224
import('./web/viewer.css?inline'),
225+
import('./themes/paper-and-ink.css?inline')
209226
])
210227
const completeHtml = viewerEntry.default
211228
.replace('</head>', `
212229
<style>${viewerCss.default}</style>
230+
<style>${paperAndInkTheme.default}</style>
213231
${Array.from(this.viewerStyles).map(style => `<style>${style}</style>`).join('\n')}
214232
</head>`)
215233
this.iframe.addEventListener('load', () => resolve(), { once: true })
@@ -263,6 +281,25 @@ export class PdfjsViewerElement extends HTMLElement {
263281
async attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
264282
if (oldValue === newValue) return
265283
if (!this.iframe) return
284+
285+
const optionByAttribute = {
286+
'worker-src': { key: 'workerSrc', fallback: DEFAULTS.workerSrc },
287+
'debugger-src': { key: 'debuggerSrc', fallback: DEFAULTS.debuggerSrc },
288+
'c-map-url': { key: 'cMapUrl', fallback: DEFAULTS.cMapUrl },
289+
'icc-url': { key: 'iccUrl', fallback: DEFAULTS.iccUrl },
290+
'image-resources-path': { key: 'imageResourcesPath', fallback: DEFAULTS.imageResourcesPath },
291+
'sandbox-bundle-src': { key: 'sandboxBundleSrc', fallback: DEFAULTS.sandboxBundleSrc },
292+
'standard-font-data-url': { key: 'standardFontDataUrl', fallback: DEFAULTS.standardFontDataUrl },
293+
'wasm-url': { key: 'wasmUrl', fallback: DEFAULTS.wasmUrl }
294+
} as const
295+
296+
if (name in optionByAttribute) {
297+
const viewerOptions = this.iframe.contentWindow?.PDFViewerApplicationOptions
298+
const { key, fallback } = optionByAttribute[name as keyof typeof optionByAttribute]
299+
viewerOptions?.set(key, newValue || fallback)
300+
return
301+
}
302+
266303
switch (name) {
267304
case 'src': {
268305
const viewerApp = this.iframe.contentWindow?.PDFViewerApplication
@@ -286,11 +323,6 @@ export class PdfjsViewerElement extends HTMLElement {
286323
case 'viewer-css-theme':
287324
this.applyViewerTheme()
288325
return
289-
case 'worker-src': {
290-
const viewerOptions = this.iframe.contentWindow?.PDFViewerApplicationOptions
291-
viewerOptions?.set('workerSrc', newValue || DEFAULTS.workerSrc)
292-
return
293-
}
294326
default:
295327
await this.applyIframeHash()
296328
}

0 commit comments

Comments
 (0)