pdfjs-viewer-element 3.1.0 → 3.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -14,11 +14,11 @@ Supported in all [major browsers](https://caniuse.com/custom-elementsv1), and wo
14
14
 
15
15
  ## Features
16
16
 
17
- - Standalone web component with no runtime dependencies
17
+ - Standalone isolated web component with no runtime dependencies
18
18
  - Drop-in, iframe-based PDF.js default viewer for any web app
19
19
  - Works with same-origin and cross-origin PDF documents
20
20
  - Configure via attributes and URL parameters (page, zoom, search, pagemode, locale)
21
- - Programmatic access to `PDFViewerApplication` and `PDFViewerApplicationOptions` via the `initialized` event
21
+ - Programmatic access to `PDFViewerApplication` via the `initPromise` public property
22
22
  - Theme control (automatic/light/dark) plus custom CSS injection
23
23
  - Locale override support using PDF.js viewer locales
24
24
  - Supports all modern browsers and most JS frameworks
@@ -31,7 +31,11 @@ Supported in all [major browsers](https://caniuse.com/custom-elementsv1), and wo
31
31
 
32
32
  [CodePen demo](https://codepen.io/redrobot753/pen/bNwVVvp)
33
33
 
34
- [Various use cases](https://github.com/alekswebnet/pdfjs-viewer-element/tree/master/demo)
34
+ [CodePen demo with React](https://codepen.io/redrobot753/pen/xbEwNrO)
35
+
36
+ [CodePen demo with Vue](https://codepen.io/redrobot753/pen/JoRYqwN)
37
+
38
+ [Usage examples](https://github.com/alekswebnet/pdfjs-viewer-element/tree/master/demo)
35
39
 
36
40
  ## Install
37
41
 
@@ -58,13 +62,18 @@ import 'pdfjs-viewer-element'
58
62
 
59
63
  ```html
60
64
  <pdfjs-viewer-element
61
- src="/sample.pdf"
65
+ src="https://alekswebnet.github.io/sample-pdf-with-images.pdf"
62
66
  style="height: 100dvh">
63
67
  </pdfjs-viewer-element>
64
68
  ```
65
69
 
66
70
  The element is block-level and needs an explicit height.
67
71
 
72
+ ### Framework usage
73
+
74
+ - [React integration example](https://codepen.io/redrobot753/pen/xbEwNrO)
75
+ - [Vue integration example](https://codepen.io/redrobot753/pen/JoRYqwN)
76
+
68
77
  ## Attributes
69
78
 
70
79
  | Attribute | Description | Default |
@@ -77,8 +86,9 @@ The element is block-level and needs an explicit height.
77
86
  | `zoom` | Zoom level (for example `auto`, `page-width`, `200%`). | `''` |
78
87
  | `pagemode` | Sidebar mode: `thumbs`, `bookmarks`, `attachments`, `layers`, `none`. | `none` |
79
88
  | `locale` | Viewer UI locale (for example `en-US`, `de`, `uk`). [Available locales](https://github.com/mozilla/pdf.js/tree/master/l10n) | `''` |
89
+ | `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` |
80
90
  | `viewer-css-theme` | Viewer theme: `AUTOMATIC`, `LIGHT`, `DARK`. | `AUTOMATIC` |
81
- | `worker-src` | PDF.js worker URL override. | `https://cdn.jsdelivr.net/npm/pdfjs-dist@5.4.624/build/pdf.worker.min.mjs` |
91
+ | `worker-src` | PDF.js worker URL override. | `<package-url>/pdf.worker.min.mjs` |
82
92
 
83
93
  Play with attributes on [API docs page](https://alekswebnet.github.io/pdfjs-viewer-element/#api).
84
94
 
@@ -92,6 +102,41 @@ Most attributes can be updated dynamically:
92
102
  - `worker-src` updates viewer options for subsequent document loads.
93
103
  - `locale` rebuilds the viewer so localization resources can be applied.
94
104
 
105
+ ## Worker source
106
+
107
+ By default, the component resolves `worker-src` to the worker shipped with this package (`pdf.worker.min.mjs` in `dist`).
108
+
109
+ Set `worker-src` only if you want to serve the worker from a custom location (for example your own CDN or static assets path).
110
+
111
+ - The URL must point to a valid PDF.js module worker file.
112
+ - The worker version should match the bundled PDF.js version.
113
+
114
+ ```html
115
+ <pdfjs-viewer-element
116
+ src="/file.pdf"
117
+ worker-src="https://cdn.jsdelivr.net/npm/pdfjs-dist@5.5.207/build/pdf.worker.min.mjs">
118
+ </pdfjs-viewer-element>
119
+ ```
120
+
121
+ ## Locale source template
122
+
123
+ Use `locale-src-template` when you need to load localization files from a custom host.
124
+
125
+ - The template must include `{locale}`.
126
+ - `{locale}` is replaced by the `locale` attribute value (for example `de`, `uk`, `en-US`).
127
+ - If `locale` is not set, no locale file is loaded.
128
+ - Changes to `locale-src-template` are applied when the viewer is (re)initialized, for example after setting/changing `locale`.
129
+
130
+ Example:
131
+
132
+ ```html
133
+ <pdfjs-viewer-element
134
+ src="/file.pdf"
135
+ locale="de"
136
+ locale-src-template="https://cdn.example.com/pdfjs-locales/{locale}/viewer.ftl">
137
+ </pdfjs-viewer-element>
138
+ ```
139
+
95
140
  ## Viewer CSS theme
96
141
 
97
142
  Use `viewer-css-theme` attribute to set light or dark theme manually:
@@ -106,9 +151,10 @@ Use `viewer-css-theme` attribute to set light or dark theme manually:
106
151
  Runtime example:
107
152
 
108
153
  ```javascript
109
- const viewer = document.querySelector('pdfjs-viewer-element')
110
- viewer.setAttribute('viewer-css-theme', 'DARK')
111
- viewer.setAttribute('viewer-css-theme', 'AUTOMATIC')
154
+ const viewerElement = document.querySelector('pdfjs-viewer-element')
155
+ viewerElement.setAttribute('viewer-css-theme', 'DARK')
156
+ viewerElement.setAttribute('viewer-css-theme', 'LIGHT')
157
+ viewerElement.setAttribute('viewer-css-theme', 'AUTOMATIC')
112
158
  ```
113
159
 
114
160
  ## Viewer custom styles
@@ -121,8 +167,8 @@ You can add your own CSS rules to the viewer application using `injectViewerStyl
121
167
  ```
122
168
 
123
169
  ```javascript
124
- const viewer = document.querySelector('#viewer')
125
- viewer.injectViewerStyles(`
170
+ const viewerElement = document.querySelector('#viewer')
171
+ viewerElement.injectViewerStyles(`
126
172
  #toolbarViewerMiddle, #toolbarViewerRight { display: none; }
127
173
  `)
128
174
  ```
@@ -152,89 +198,63 @@ Build your own theme with viewer custom variables and inject it via `injectViewe
152
198
  }
153
199
  ```
154
200
 
155
- ## Methods
201
+ ## Methods and Public properties
202
+
203
+ Methods:
156
204
 
157
205
  `injectViewerStyles(styles: string)` - Adds custom CSS to the viewer now (when ready) and for future rebuilds.
158
206
 
159
- ## Programmatic access to PDF.js
160
-
161
- ```html
162
- <pdfjs-viewer-element></pdfjs-viewer-element>
163
- ```
207
+ Example (`injectViewerStyles`):
164
208
 
165
209
  ```javascript
166
- // Open PDF file programmatically accessing the viewer application
167
- document.addEventListener('DOMContentLoaded', async () => {
168
- document.querySelector('pdfjs-viewer-element').addEventListener('initialized', (event) => {
169
- const { viewerApp, viewerOptions } = event.detail
170
- viewerApp.open({ data: pdfData })
171
- })
172
- })
173
- ```
210
+ const viewerElement = document.querySelector('pdfjs-viewer-element')
174
211
 
175
- You can also react to source changes dynamically:
176
-
177
- ```javascript
178
- const viewer = document.querySelector('pdfjs-viewer-element')
179
- viewer.setAttribute('src', '/another-file.pdf')
212
+ await viewerElement.injectViewerStyles(`
213
+ #toolbarViewerRight { display: none; }
214
+ #findbar { border-top: 2px solid #0200a8; }
215
+ `)
180
216
  ```
181
217
 
182
- ## Events
183
-
184
- `initialized` - Fired after the PDF.js viewer is ready (after `PDFViewerApplication.initializedPromise` resolves). The event `detail` contains:
185
-
186
- - `viewerApp` (`PDFViewerApplication`)
187
- - `viewerOptions` (`PDFViewerApplicationOptions`)
218
+ Public properties:
188
219
 
189
- The event is emitted each time the internal viewer is rebuilt (for example after changing `locale`).
220
+ `initPromise: Promise<InitializationData>` - Resolves after internal viewer is completely loaded and initialized, returning `{ viewerApp }`, that gives a programmatic access to PDF.js viewer app (PDFViewerApplication).
190
221
 
191
- ## Migration notes
222
+ Example (`initPromise`):
192
223
 
193
- If you are upgrading from an older version:
194
-
195
- - `viewer-extra-styles` and `viewer-extra-styles-urls` attributes are removed.
196
- - Use `injectViewerStyles(styles)` instead of style attributes.
197
- - Use the `initialized` event for `viewerApp` / `viewerOptions` access.
198
- - Runtime `src` updates are supported with `setAttribute('src', ...)`.
199
-
200
- ## Accessibility
224
+ ```javascript
225
+ const viewerElement = document.querySelector('pdfjs-viewer-element')
226
+ const { viewerApp } = await viewerElement.initPromise
201
227
 
202
- Use `iframe-title` to add a title to the `iframe` element and improve accessibility.
228
+ viewerApp.open({ url: '/sample.pdf' })
229
+ ```
203
230
 
204
- ## Known issues
231
+ `iframe: PdfjsViewerElementIframe` - Public reference to the internal `iframe` element. Useful when you need direct access to `contentWindow`/`contentDocument`.
205
232
 
206
- ### The `.mjs` files support
233
+ Example (`iframe`):
207
234
 
208
- Since v4 PDF.js requires `.mjs` files support, make sure your server has it.
235
+ ```javascript
236
+ const viewerElement = document.querySelector('pdfjs-viewer-element')
209
237
 
210
- In case of `nginx` this may cause errors, see https://github.com/mozilla/pdf.js/issues/17296
238
+ // Access iframe window directly when needed.
239
+ const iframeWindow = viewerElement.iframe.contentWindow
211
240
 
212
- Add `.mjs` files support for `nginx` example:
241
+ // Read current location hash applied to the viewer.
242
+ console.log(iframeWindow.location.hash)
213
243
 
214
- ```bash
215
- server {
216
- # ...
217
-
218
- location / {
219
- root /usr/share/nginx/html;
220
- index index.html;
221
-
222
- location ~* \.mjs$ {
223
- types {
224
- text/javascript mjs;
225
- }
226
- }
227
- }
228
- }
244
+ // Inspect iframe document title.
245
+ console.log(viewerElement.iframe.contentDocument.title)
229
246
  ```
230
247
 
231
- ## Support via Ko-fi
248
+ You can also react to source changes dynamically:
232
249
 
233
- If you find `pdfjs-viewer-element` useful and want to support its development, consider making a donation via Ko-fi:
250
+ ```javascript
251
+ const viewerElement = document.querySelector('pdfjs-viewer-element')
252
+ viewerElement.setAttribute('src', '/another-file.pdf')
253
+ ```
234
254
 
235
- [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/oleksandrshevchuk)
255
+ ## Accessibility
236
256
 
237
- > ❤️ Your support helps with maintenance, bug fixes, and long-term improvements.
257
+ Use `iframe-title` to add a title to the `iframe` element and improve accessibility.
238
258
 
239
259
  ## License
240
260
  [MIT](http://opensource.org/licenses/MIT)