Skip to content

Commit c99fe38

Browse files
committed
Add resource attrs #46
1 parent 9454e74 commit c99fe38

File tree

4 files changed

+175
-56
lines changed

4 files changed

+175
-56
lines changed

README.md

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Supported in all [major browsers](https://caniuse.com/custom-elementsv1), and wo
1919
- Works with same-origin and cross-origin PDF documents
2020
- Configure via attributes and URL parameters (page, zoom, search, pagemode, locale)
2121
- Programmatic access to `PDFViewerApplication` via the `initPromise` public property
22-
- Theme control (automatic/light/dark) plus custom CSS injection
22+
- Built-in Paper & Ink default theme, with theme control (automatic/light/dark) and custom CSS injection
2323
- Locale override support using PDF.js viewer locales
2424
- Supports all modern browsers and most JS frameworks
2525

@@ -89,6 +89,13 @@ The element is block-level and needs an explicit height.
8989
| `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` |
9090
| `viewer-css-theme` | Viewer theme: `AUTOMATIC`, `LIGHT`, `DARK`. | `AUTOMATIC` |
9191
| `worker-src` | PDF.js worker URL override. | `<package-url>/pdf.worker.min.mjs` |
92+
| `debugger-src` | PDF.js debugger script URL (`debuggerSrc` option). | `./debugger.mjs` |
93+
| `c-map-url` | CMap directory URL (`cMapUrl` option). | `../web/cmaps/` |
94+
| `icc-url` | ICC profile directory URL (`iccUrl` option). | `../web/iccs/` |
95+
| `image-resources-path` | Image resources directory (`imageResourcesPath` option). | `./images/` |
96+
| `sandbox-bundle-src` | Sandbox bundle URL (`sandboxBundleSrc` option). | `../build/pdf.sandbox.mjs` |
97+
| `standard-font-data-url` | Standard fonts directory (`standardFontDataUrl` option). | `../web/standard_fonts/` |
98+
| `wasm-url` | WASM assets directory (`wasmUrl` option). | `../web/wasm/` |
9299

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

@@ -99,7 +106,7 @@ Most attributes can be updated dynamically:
99106
- `src` updates by calling PDF.js `open({ url })` without rebuilding the viewer.
100107
- `page`, `search`, `phrase`, `zoom`, `pagemode` update via hash parameters.
101108
- `viewer-css-theme` updates the viewer theme at runtime.
102-
- `worker-src` updates viewer options for subsequent document loads.
109+
- `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.
103110
- `locale` rebuilds the viewer so localization resources can be applied.
104111

105112
## Worker source
@@ -139,6 +146,8 @@ Example:
139146

140147
## Viewer CSS theme
141148

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

144153
```html
@@ -157,6 +166,24 @@ viewerElement.setAttribute('viewer-css-theme', 'LIGHT')
157166
viewerElement.setAttribute('viewer-css-theme', 'AUTOMATIC')
158167
```
159168

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

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

src/pdfjs-viewer-element.ts

Lines changed: 36 additions & 6 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)
@@ -265,6 +281,25 @@ export class PdfjsViewerElement extends HTMLElement {
265281
async attributeChangedCallback(name: string, oldValue: string | null, newValue: string | null) {
266282
if (oldValue === newValue) return
267283
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+
268303
switch (name) {
269304
case 'src': {
270305
const viewerApp = this.iframe.contentWindow?.PDFViewerApplication
@@ -288,11 +323,6 @@ export class PdfjsViewerElement extends HTMLElement {
288323
case 'viewer-css-theme':
289324
this.applyViewerTheme()
290325
return
291-
case 'worker-src': {
292-
const viewerOptions = this.iframe.contentWindow?.PDFViewerApplicationOptions
293-
viewerOptions?.set('workerSrc', newValue || DEFAULTS.workerSrc)
294-
return
295-
}
296326
default:
297327
await this.applyIframeHash()
298328
}

0 commit comments

Comments
 (0)