pdfjs-viewer-element 3.1.0 → 3.1.1

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
@@ -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 |
@@ -106,9 +115,10 @@ Use `viewer-css-theme` attribute to set light or dark theme manually:
106
115
  Runtime example:
107
116
 
108
117
  ```javascript
109
- const viewer = document.querySelector('pdfjs-viewer-element')
110
- viewer.setAttribute('viewer-css-theme', 'DARK')
111
- viewer.setAttribute('viewer-css-theme', 'AUTOMATIC')
118
+ const viewerElement = document.querySelector('pdfjs-viewer-element')
119
+ viewerElement.setAttribute('viewer-css-theme', 'DARK')
120
+ viewerElement.setAttribute('viewer-css-theme', 'LIGHT')
121
+ viewerElement.setAttribute('viewer-css-theme', 'AUTOMATIC')
112
122
  ```
113
123
 
114
124
  ## Viewer custom styles
@@ -121,8 +131,8 @@ You can add your own CSS rules to the viewer application using `injectViewerStyl
121
131
  ```
122
132
 
123
133
  ```javascript
124
- const viewer = document.querySelector('#viewer')
125
- viewer.injectViewerStyles(`
134
+ const viewerElement = document.querySelector('#viewer')
135
+ viewerElement.injectViewerStyles(`
126
136
  #toolbarViewerMiddle, #toolbarViewerRight { display: none; }
127
137
  `)
128
138
  ```
@@ -152,89 +162,63 @@ Build your own theme with viewer custom variables and inject it via `injectViewe
152
162
  }
153
163
  ```
154
164
 
155
- ## Methods
165
+ ## Methods and Public properties
156
166
 
157
- `injectViewerStyles(styles: string)` - Adds custom CSS to the viewer now (when ready) and for future rebuilds.
167
+ Methods:
158
168
 
159
- ## Programmatic access to PDF.js
169
+ `injectViewerStyles(styles: string)` - Adds custom CSS to the viewer now (when ready) and for future rebuilds.
160
170
 
161
- ```html
162
- <pdfjs-viewer-element></pdfjs-viewer-element>
163
- ```
171
+ Example (`injectViewerStyles`):
164
172
 
165
173
  ```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
- ```
174
-
175
- You can also react to source changes dynamically:
174
+ const viewerElement = document.querySelector('pdfjs-viewer-element')
176
175
 
177
- ```javascript
178
- const viewer = document.querySelector('pdfjs-viewer-element')
179
- viewer.setAttribute('src', '/another-file.pdf')
176
+ await viewerElement.injectViewerStyles(`
177
+ #toolbarViewerRight { display: none; }
178
+ #findbar { border-top: 2px solid #0200a8; }
179
+ `)
180
180
  ```
181
181
 
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`)
182
+ Public properties:
188
183
 
189
- The event is emitted each time the internal viewer is rebuilt (for example after changing `locale`).
184
+ `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
185
 
191
- ## Migration notes
186
+ Example (`initPromise`):
192
187
 
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
188
+ ```javascript
189
+ const viewerElement = document.querySelector('pdfjs-viewer-element')
190
+ const { viewerApp } = await viewerElement.initPromise
201
191
 
202
- Use `iframe-title` to add a title to the `iframe` element and improve accessibility.
192
+ viewerApp.open({ url: '/sample.pdf' })
193
+ ```
203
194
 
204
- ## Known issues
195
+ `iframe: PdfjsViewerElementIframe` - Public reference to the internal `iframe` element. Useful when you need direct access to `contentWindow`/`contentDocument`.
205
196
 
206
- ### The `.mjs` files support
197
+ Example (`iframe`):
207
198
 
208
- Since v4 PDF.js requires `.mjs` files support, make sure your server has it.
199
+ ```javascript
200
+ const viewerElement = document.querySelector('pdfjs-viewer-element')
209
201
 
210
- In case of `nginx` this may cause errors, see https://github.com/mozilla/pdf.js/issues/17296
202
+ // Access iframe window directly when needed.
203
+ const iframeWindow = viewerElement.iframe.contentWindow
211
204
 
212
- Add `.mjs` files support for `nginx` example:
205
+ // Read current location hash applied to the viewer.
206
+ console.log(iframeWindow.location.hash)
213
207
 
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
- }
208
+ // Inspect iframe document title.
209
+ console.log(viewerElement.iframe.contentDocument.title)
229
210
  ```
230
211
 
231
- ## Support via Ko-fi
212
+ You can also react to source changes dynamically:
232
213
 
233
- If you find `pdfjs-viewer-element` useful and want to support its development, consider making a donation via Ko-fi:
214
+ ```javascript
215
+ const viewerElement = document.querySelector('pdfjs-viewer-element')
216
+ viewer.setAttribute('src', '/another-file.pdf')
217
+ ```
234
218
 
235
- [![ko-fi](https://ko-fi.com/img/githubbutton_sm.svg)](https://ko-fi.com/oleksandrshevchuk)
219
+ ## Accessibility
236
220
 
237
- > ❤️ Your support helps with maintenance, bug fixes, and long-term improvements.
221
+ Use `iframe-title` to add a title to the `iframe` element and improve accessibility.
238
222
 
239
223
  ## License
240
224
  [MIT](http://opensource.org/licenses/MIT)