@@ -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 ` and ` PDFViewerApplicationOptions ` via the ` initialized ` event
22- - Theme control (automatic/light/dark) plus custom CSS injection or external stylesheets
22+ - Theme control (automatic/light/dark) plus custom CSS injection
2323- Locale override support using PDF.js viewer locales
2424- Supports all modern browsers and most JS frameworks
2525
@@ -29,6 +29,8 @@ Supported in all [major browsers](https://caniuse.com/custom-elementsv1), and wo
2929
3030[ API playground] ( https://alekswebnet.github.io/pdfjs-viewer-element/#api )
3131
32+ [ CodePen demo] ( https://codepen.io/redrobot753/pen/bNwVVvp )
33+
3234[ Various use cases] ( https://github.com/alekswebnet/pdfjs-viewer-element/tree/master/demo )
3335
3436## Install
@@ -55,34 +57,40 @@ import 'pdfjs-viewer-element'
5557## Usage
5658
5759``` html
58- <pdfjs-viewer-element src =" path-to/file.pdf" ></pdfjs-viewer-element >
60+ <pdfjs-viewer-element
61+ src =" /sample.pdf"
62+ style =" height : 100 dvh" >
63+ </pdfjs-viewer-element >
5964```
6065
61- ## Attributes
62-
63- ` src ` - PDF file URL
64-
65- ` iframe-title ` - The title of the ` iframe ` element, required for better accessibility
66-
67- ` page ` - Page number.
68-
69- ` search ` - Search text.
70-
71- ` phrase ` - Search by phrase, ` true ` to enable.
72-
73- ` zoom ` - Zoom level.
66+ The element is block-level and needs an explicit height.
7467
75- ` pagemode ` - Page mode, ` thumbs | bookmarks | attachments | layers | none ` .
68+ ## Attributes
7669
77- ` locale ` - Specifies which language to use in the viewer UI, ` en-US | ... ` . [ Available locales] ( https://github.com/mozilla/pdf.js/tree/master/l10n )
70+ | Attribute | Description | Default |
71+ | --- | --- | --- |
72+ | ` src ` | PDF file URL. | ` '' ` |
73+ | ` iframe-title ` | Title for the internal ` iframe ` (recommended for accessibility). | ` PDF viewer window ` |
74+ | ` page ` | Page number. | ` '' ` |
75+ | ` search ` | Search query text. | ` '' ` |
76+ | ` phrase ` | Phrase search mode, set to ` true ` to enable phrase matching. | ` '' ` |
77+ | ` zoom ` | Zoom level (for example ` auto ` , ` page-width ` , ` 200% ` ). | ` '' ` |
78+ | ` pagemode ` | Sidebar mode: ` thumbs ` , ` bookmarks ` , ` attachments ` , ` layers ` , ` none ` . | ` none ` |
79+ | ` locale ` | Viewer UI locale (for example ` en-US ` , ` de ` , ` uk ` ). [ Available locales] ( https://github.com/mozilla/pdf.js/tree/master/l10n ) | ` '' ` |
80+ | ` 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 ` |
7882
79- ` viewer-css-theme ` - Apply automatic, light, or dark theme, ` AUTOMATIC | LIGHT | DARK `
83+ Play with attributes on [ API docs page ] ( https://alekswebnet.github.io/pdfjs-viewer-element/#api ) .
8084
81- ` viewer-extra-styles ` - Add your CSS rules to the viewer application, pass a string with styles.
85+ ## Runtime updates
8286
83- ` viewer-extra-styles-urls ` - Add external CSS files to the viewer application, pass an array with URLs.
87+ Most attributes can be updated dynamically:
8488
85- Play with attributes on [ API docs page] ( https://alekswebnet.github.io/pdfjs-viewer-element/#api ) .
89+ - ` src ` updates by calling PDF.js ` open({ url }) ` without rebuilding the viewer.
90+ - ` page ` , ` search ` , ` phrase ` , ` zoom ` , ` pagemode ` update via hash parameters.
91+ - ` viewer-css-theme ` updates the viewer theme at runtime.
92+ - ` worker-src ` updates viewer options for subsequent document loads.
93+ - ` locale ` rebuilds the viewer so localization resources can be applied.
8694
8795## Viewer CSS theme
8896
@@ -95,18 +103,33 @@ Use `viewer-css-theme` attribute to set light or dark theme manually:
95103</pdfjs-viewer-element >
96104```
97105
98- ## Viewer custom styles
106+ Runtime example:
107+
108+ ``` javascript
109+ const viewer = document .querySelector (' pdfjs-viewer-element' )
110+ viewer .setAttribute (' viewer-css-theme' , ' DARK' )
111+ viewer .setAttribute (' viewer-css-theme' , ' AUTOMATIC' )
112+ ```
113+
114+ ## Viewer custom styles
99115
100- You can add your own CSS rules to the viewer application using ` viewer-extra- styles` or ` viewer-extra-styles-urls ` attribute :
116+ You can add your own CSS rules to the viewer application using ` injectViewerStyles( styles: string) ` :
101117
102118``` html
103- <pdfjs-viewer-element
104- src =" /file.pdf"
105- viewer-extra-styles =" #toolbarViewerMiddle { display: none; }"
106- viewer-extra-styles-urls =" ['/demo/viewer-custom-theme.css']" >
119+ <pdfjs-viewer-element id =" viewer" src =" /file.pdf" >
107120</pdfjs-viewer-element >
108121```
109- Build your own theme with viewer's custom variables and ` viewer-extra-styles-urls ` attribute:
122+
123+ ``` javascript
124+ const viewer = document .querySelector (' #viewer' )
125+ viewer .injectViewerStyles (`
126+ #toolbarViewerMiddle, #toolbarViewerRight { display: none; }
127+ ` )
128+ ```
129+
130+ ` injectViewerStyles(...) ` applies styles immediately when the viewer document is ready, and keeps them for future rebuilds.
131+
132+ Build your own theme with viewer custom variables and inject it via ` injectViewerStyles(...) ` :
110133
111134``` css
112135:root {
@@ -129,7 +152,11 @@ Build your own theme with viewer's custom variables and `viewer-extra-styles-url
129152}
130153```
131154
132- ## PDF.js Viewer Application and Options
155+ ## Methods
156+
157+ ` injectViewerStyles(styles: string) ` - Adds custom CSS to the viewer now (when ready) and for future rebuilds.
158+
159+ ## Programmatic access to PDF.js
133160
134161``` html
135162<pdfjs-viewer-element ></pdfjs-viewer-element >
@@ -145,13 +172,31 @@ document.addEventListener('DOMContentLoaded', async () => {
145172})
146173```
147174
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' )
180+ ```
181+
148182## Events
149183
150184` initialized ` - Fired after the PDF.js viewer is ready (after ` PDFViewerApplication.initializedPromise ` resolves). The event ` detail ` contains:
151185
152186- ` viewerApp ` (` PDFViewerApplication ` )
153187- ` viewerOptions ` (` PDFViewerApplicationOptions ` )
154188
189+ The event is emitted each time the internal viewer is rebuilt (for example after changing ` locale ` ).
190+
191+ ## Migration notes
192+
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+
155200## Accessibility
156201
157202Use ` iframe-title ` to add a title to the ` iframe ` element and improve accessibility.
0 commit comments