@@ -14,28 +14,23 @@ Supported in all [major browsers](https://caniuse.com/custom-elementsv1), and wo
1414
1515## Features
1616
17- ✅ Standalone web component with no runtime dependencies
18-
19- ✅ Drop-in, iframe-based PDF.js default viewer for any web app
20-
21- ✅ Works with same-origin and cross-origin PDF documents
22-
23- ✅ Configure via attributes and URL parameters (page, zoom, search, pagemode, locale)
24-
25- ✅ Programmatic access to ` PDFViewerApplication ` and ` PDFViewerApplicationOptions ` via the ` initialized ` event
26-
27- ✅ Theme control (automatic/light/dark) plus custom CSS injection or external stylesheets
28-
29- ✅ Locale override support using PDF.js viewer locales
30-
31- ✅ Supports all modern browsers and most JS frameworks
17+ - Standalone web component with no runtime dependencies
18+ - Drop-in, iframe-based PDF.js default viewer for any web app
19+ - Works with same-origin and cross-origin PDF documents
20+ - Configure via attributes and URL parameters (page, zoom, search, pagemode, locale)
21+ - Programmatic access to ` PDFViewerApplication ` and ` PDFViewerApplicationOptions ` via the ` initialized ` event
22+ - Theme control (automatic/light/dark) plus custom CSS injection
23+ - Locale override support using PDF.js viewer locales
24+ - Supports all modern browsers and most JS frameworks
3225
3326## Docs
3427
3528[ Getting started] ( https://alekswebnet.github.io/pdfjs-viewer-element/ )
3629
3730[ API playground] ( https://alekswebnet.github.io/pdfjs-viewer-element/#api )
3831
32+ [ CodePen demo] ( https://codepen.io/redrobot753/pen/bNwVVvp )
33+
3934[ Various use cases] ( https://github.com/alekswebnet/pdfjs-viewer-element/tree/master/demo )
4035
4136## Install
@@ -62,34 +57,40 @@ import 'pdfjs-viewer-element'
6257## Usage
6358
6459``` html
65- <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 >
6664```
6765
68- ## Attributes
69-
70- ` src ` - PDF file URL
71-
72- ` iframe-title ` - The title of the ` iframe ` element, required for better accessibility
73-
74- ` page ` - Page number.
75-
76- ` search ` - Search text.
77-
78- ` phrase ` - Search by phrase, ` true ` to enable.
66+ The element is block-level and needs an explicit height.
7967
80- ` zoom ` - Zoom level.
68+ ## Attributes
8169
82- ` pagemode ` - Page mode, ` thumbs | bookmarks | attachments | layers | none ` .
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 ` |
8382
84- ` locale ` - Specifies which language to use in the viewer UI, ` en-US | ... ` . [ Available locales ] ( https://github.com/mozilla/pdf.js/tree/master/l10n )
83+ Play with attributes on [ API docs page ] ( https://alekswebnet. github.io/pdfjs-viewer-element/#api ) .
8584
86- ` viewer-css-theme ` - Apply automatic, light, or dark theme, ` AUTOMATIC | LIGHT | DARK `
85+ ## Runtime updates
8786
88- ` viewer-extra-styles ` - Add your CSS rules to the viewer application, pass a string with styles.
87+ Most attributes can be updated dynamically:
8988
90- ` viewer-extra-styles-urls ` - Add external CSS files to the viewer application, pass an array with URLs.
91-
92- 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.
9394
9495## Viewer CSS theme
9596
@@ -102,18 +103,33 @@ Use `viewer-css-theme` attribute to set light or dark theme manually:
102103</pdfjs-viewer-element >
103104```
104105
105- ## 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
106115
107- 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) ` :
108117
109118``` html
110- <pdfjs-viewer-element
111- src =" /file.pdf"
112- viewer-extra-styles =" #toolbarViewerMiddle { display: none; }"
113- viewer-extra-styles-urls =" ['/demo/viewer-custom-theme.css']" >
119+ <pdfjs-viewer-element id =" viewer" src =" /file.pdf" >
114120</pdfjs-viewer-element >
115121```
116- 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(...) ` :
117133
118134``` css
119135:root {
@@ -136,7 +152,11 @@ Build your own theme with viewer's custom variables and `viewer-extra-styles-url
136152}
137153```
138154
139- ## 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
140160
141161``` html
142162<pdfjs-viewer-element ></pdfjs-viewer-element >
@@ -152,13 +172,31 @@ document.addEventListener('DOMContentLoaded', async () => {
152172})
153173```
154174
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+
155182## Events
156183
157184` initialized ` - Fired after the PDF.js viewer is ready (after ` PDFViewerApplication.initializedPromise ` resolves). The event ` detail ` contains:
158185
159186- ` viewerApp ` (` PDFViewerApplication ` )
160187- ` viewerOptions ` (` PDFViewerApplicationOptions ` )
161188
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+
162200## Accessibility
163201
164202Use ` iframe-title ` to add a title to the ` iframe ` element and improve accessibility.
0 commit comments