Skip to content

Commit f412b7e

Browse files
committed
Update demo & readme
1 parent 50d4a76 commit f412b7e

File tree

7 files changed

+57
-27
lines changed

7 files changed

+57
-27
lines changed

README.md

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,14 @@ 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 or external stylesheets
23+
- Locale override support using PDF.js viewer locales
24+
- Supports all modern browsers and most JS frameworks
3225

3326
## Docs
3427

demo/extra-styles-urls.html

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,14 @@
1919
style="height: 100dvh"
2020
>
2121
</pdfjs-viewer-element>
22+
23+
<script>
24+
document.addEventListener('DOMContentLoaded', async () => {
25+
// Create a very simple toolbar by hiding the middle and right toolbar parts.
26+
document.querySelector('pdfjs-viewer-element').injectViewerStyles(`
27+
#toolbarViewerMiddle, #toolbarViewerRight { display: none; }
28+
`)
29+
})
30+
</script>
2231
</body>
2332
</html>

index.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<script type="module" src="./index.js"></script>
1010
</head>
1111
<body style="margin: 0">
12-
<!-- <pdfjs-viewer-element
12+
<pdfjs-viewer-element
1313
src="/fake-file.pdf"
1414
style="height: 600px">
1515
</pdfjs-viewer-element>
@@ -21,7 +21,7 @@
2121
page="2"
2222
style="height: 600px">
2323
</pdfjs-viewer-element>
24-
<button onclick="toggleDownloadButton(document.querySelector('#hideOpenFileViewer'))">Toggle download button</button> -->
24+
<button onclick="toggleDownloadButton(document.querySelector('#hideOpenFileViewer'))">Toggle download button</button>
2525

2626
<pdfjs-viewer-element
2727
id="themedViewer"
@@ -50,10 +50,10 @@
5050
<button onclick="document.querySelector('#themedViewer').setAttribute('src', '/sample-pdf-10MB.pdf')">Load 10MB PDF</button>
5151
<hr>
5252

53-
<!-- <pdfjs-viewer-element
53+
<pdfjs-viewer-element
5454
id="base-viewer"
5555
locale="uk"
5656
style="height: clamp(400px, 80dvh, 600px)">
57-
</pdfjs-viewer-element> -->
57+
</pdfjs-viewer-element>
5858
</body>
5959
</html>

index.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ const pdfData = Uint8Array.from(atob(
1313
'MDAwIG4gCjAwMDAwMDAzODAgMDAwMDAgbiAKdHJhaWxlcgo8PAogIC9TaXplIDYKICAvUm9v' +
1414
'dCAxIDAgUgo+PgpzdGFydHhyZWYKNDkyCiUlRU9G'), (m) => m.codePointAt(0));
1515

16-
// document.addEventListener('DOMContentLoaded', async () => {
17-
// const { viewerApp } = await document.querySelector('#base-viewer').initialize()
18-
// viewerApp.open({ data: pdfData })
19-
// })
16+
document.addEventListener('DOMContentLoaded', async () => {
17+
document.querySelector('#base-viewer').addEventListener('initialized', (event) => {
18+
const { viewerApp } = event.detail
19+
viewerApp.open({ data: pdfData })
20+
})
21+
})
2022

2123
const toggleDownloadButton = async (viewerElement) => {
22-
const { viewerApp } = await viewerElement.initialize()
23-
viewerApp.appConfig.toolbar.download.classList.toggle('hidden')
24+
viewerElement.addEventListener('initialized', (event) => {
25+
const { viewerApp } = event.detail
26+
viewerApp.appConfig.toolbar.download.classList.toggle('hidden')
27+
})
2428
}
2529

2630
window.toggleDownloadButton = toggleDownloadButton

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pdfjs-viewer-element",
3-
"version": "3.0.2",
3+
"version": "3.1.0",
44
"license": "MIT",
55
"author": {
66
"name": "Oleksandr Shevchuk",

src/pdfjs-viewer-element.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,26 @@ export class PdfjsViewerElement extends HTMLElement {
6868
}
6969
}
7070

71+
private appendRuntimeStyle(styles: string) {
72+
const doc = this.iframe?.contentDocument
73+
if (!doc?.head || !styles) return
74+
75+
const exists = Array.from(doc.querySelectorAll('style'))
76+
.some((styleNode) => styleNode.textContent === styles)
77+
if (exists) return
78+
79+
const styleElement = doc.createElement('style')
80+
styleElement.setAttribute('data-pdfjs-viewer-runtime-style', 'true')
81+
styleElement.textContent = styles
82+
doc.head.appendChild(styleElement)
83+
}
84+
85+
private applyQueuedRuntimeStyles() {
86+
this.viewerStyles.forEach((styles) => {
87+
this.appendRuntimeStyle(styles)
88+
})
89+
}
90+
7191
private injectScript(value: string, type = 'module') {
7292
const doc = this.iframe.contentDocument
7393
if (!doc) return
@@ -181,6 +201,7 @@ export class PdfjsViewerElement extends HTMLElement {
181201
const viewerOptions = this.applyViewerOptions()
182202
await viewerApp?.initializedPromise
183203

204+
this.applyQueuedRuntimeStyles()
184205
this.applyIframeHash()
185206

186207
this.dispatchEvent(new CustomEvent('initialized', {
@@ -253,7 +274,9 @@ export class PdfjsViewerElement extends HTMLElement {
253274
}
254275

255276
public async injectViewerStyles(styles: string) {
256-
if (styles) this.viewerStyles.add(styles)
277+
if (!styles) return
278+
this.viewerStyles.add(styles)
279+
this.appendRuntimeStyle(styles)
257280
}
258281
}
259282

types/pdfjs-viewer-element.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export declare class PdfjsViewerElement extends HTMLElement {
1414
private getCssThemeOption;
1515
private applyIframeHash;
1616
private applyViewerTheme;
17+
private appendRuntimeStyle;
1718
private injectScript;
1819
private injectLocaleData;
1920
private cleanupLocaleResource;

0 commit comments

Comments
 (0)