Skip to content

Commit 741b53e

Browse files
authored
Merge pull request #40 from alekswebnet/39-make-this-library-standalone
Make this library standalone
2 parents e444611 + 67b1cc7 commit 741b53e

426 files changed

Lines changed: 169639 additions & 459 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 28 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,21 @@ Supported in all [major browsers](https://caniuse.com/custom-elementsv1), and wo
1414

1515
## Features
1616

17-
- Simple PDF.js viewer integration to any web application
18-
- PDF.js viewer options and parameters support, access the viewer application instance
19-
- Customize viewer styles and themes
17+
- Standalone web component with no runtime dependencies
18+
- Drop-in, iframe-based PDF.js default viewer for any web app
19+
- Configure via attributes and URL parameters (page, zoom, search, pagemode, locale, named destination)
20+
- Programmatic access to `PDFViewerApplication` and `PDFViewerApplicationOptions` via the `initialized` event
21+
- Theme control (automatic/light/dark) plus custom CSS injection or external stylesheets
22+
- Locale override support using PDF.js viewer locales
23+
- Works in modern browsers and most JS frameworks
2024

2125
## Docs
2226

2327
[Getting started](https://alekswebnet.github.io/pdfjs-viewer-element/)
2428

2529
[API playground](https://alekswebnet.github.io/pdfjs-viewer-element/#api)
2630

27-
[Usage with frameworks](https://alekswebnet.github.io/pdfjs-viewer-element/#demo)
28-
29-
[Various usecases](https://github.com/alekswebnet/pdfjs-viewer-element/tree/master/demo)
30-
31-
## How it works
32-
33-
**⚠️ This is an important part, please read this FIRST !!!**
34-
35-
**You should download and place the PDF.js prebuilt files in the project.**
36-
37-
`pdfjs-viewer-element` requires [PDF.js prebuilt](http://mozilla.github.io/pdf.js/getting_started/), that includes the generic build of PDF.js and the viewer.
38-
39-
The prebuilt comes with each PDF.js release. [PDF.JS releases](https://github.com/mozilla/pdf.js/releases)
40-
41-
✅ All v3, v4 and v5 releases are fully supported.
42-
43-
After placing the prebuild specify the path to the directory with the `viewer-path` property (`/pdfjs` by default) and PDF file URL with `src` property (should refer to the [same origin](https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#can-i-load-a-pdf-from-another-server-cross-domain-request)).
31+
[Various use cases](https://github.com/alekswebnet/pdfjs-viewer-element/tree/master/demo)
4432

4533
## Install
4634

@@ -49,8 +37,6 @@ After placing the prebuild specify the path to the directory with the `viewer-pa
4937
```bash
5038
# With npm
5139
npm install pdfjs-viewer-element
52-
# With yarn
53-
yarn add pdfjs-viewer-element
5440
# With pnpm
5541
pnpm add pdfjs-viewer-element
5642
```
@@ -68,21 +54,17 @@ import 'pdfjs-viewer-element'
6854
## Usage
6955

7056
```html
71-
<pdfjs-viewer-element src="/file.pdf" viewer-path="/pdfjs-5.3.93-dist"></pdfjs-viewer-element>
57+
<pdfjs-viewer-element src="path-to/file.pdf"></pdfjs-viewer-element>
7258
```
7359

7460
## Attributes
7561

7662
`src` - PDF file URL, should refer to the [same origin](https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#can-i-load-a-pdf-from-another-server-cross-domain-request)
7763

78-
`viewer-path` - Path to PDF.js [prebuilt](http://mozilla.github.io/pdf.js/getting_started/)
79-
8064
`iframe-title` - The title of the `iframe` element, required for better accessibility
8165

8266
`page` - Page number.
8367

84-
`nameddest` - Go to a named destination.
85-
8668
`search` - Search text.
8769

8870
`phrase` - Search by phrase, `true` to enable.
@@ -91,20 +73,6 @@ import 'pdfjs-viewer-element'
9173

9274
`pagemode` - Page mode, `thumbs | bookmarks | attachments | layers | none`.
9375

94-
`disable-worker` - Disables the worker, `true` to enable.
95-
96-
`text-layer` - Disables or reveals the text layer that is used for text selection, `off | visible | shadow | hover`.
97-
98-
`disable-font-face` - Disables standard `@font-face` font loading and uses the internal font renderer instead, `true` to enable.
99-
100-
`disable-range` - Disables HTTP range requests when fetching the document, `true` to enable.
101-
102-
`disable-stream` - Disables streaming when fetching the document, `true` to enable.
103-
104-
`disable-auto-fetch`- Disables auto fetching of the document; only gets necessary data to display the current view. Note: streaming also needs to be disabled for this to have any effect, `true` to enable.
105-
106-
`verbosity`- Specifies the verbosity level of console messages. `0` - only errors, `1` - warnings and errors, `5` - warnings, errors and information messages.
107-
10876
`locale` - Specifies which language to use in the viewer UI, `en-US | ...`. [Available locales](https://github.com/mozilla/pdf.js/tree/master/l10n)
10977

11078
`viewer-css-theme` - Apply automatic, light, or dark theme, `AUTOMATIC | LIGHT | DARK`
@@ -113,7 +81,7 @@ import 'pdfjs-viewer-element'
11381

11482
`viewer-extra-styles-urls` - Add external CSS files to the viewer application, pass an array with URLs.
11583

116-
Play with attributes on [Api docs page](https://alekswebnet.github.io/pdfjs-viewer-element/#api).
84+
Play with attributes on [API docs page](https://alekswebnet.github.io/pdfjs-viewer-element/#api).
11785

11886
## Viewer CSS theme
11987

@@ -122,7 +90,6 @@ Use `viewer-css-theme` attribute to set light or dark theme manually:
12290
```html
12391
<pdfjs-viewer-element
12492
src="/file.pdf"
125-
viewer-path="/pdfjs-5.3.93-dist"
12693
viewer-css-theme="DARK">
12794
</pdfjs-viewer-element>
12895
```
@@ -134,7 +101,6 @@ You can add your own CSS rules to the viewer application using `viewer-extra-sty
134101
```html
135102
<pdfjs-viewer-element
136103
src="/file.pdf"
137-
viewer-path="/pdfjs-5.3.93-dist"
138104
viewer-extra-styles="#toolbarViewerMiddle { display: none; }"
139105
viewer-extra-styles-urls="['/demo/viewer-custom-theme.css']">
140106
</pdfjs-viewer-element>
@@ -162,23 +128,30 @@ Build your own theme with viewer's custom variables and `viewer-extra-styles-url
162128
}
163129
```
164130

165-
## PDF.js Viewer Application
166-
167-
`initialize` - using this method you can access PDFViewerApplication and use methods and events of PDF.js default viewer
131+
## PDF.js Viewer Application and Options
168132

169133
```html
170-
<pdfjs-viewer-element viewer-path="/pdfjs-5.3.93-dist"></pdfjs-viewer-element>
134+
<pdfjs-viewer-element></pdfjs-viewer-element>
171135
```
172136

173137
```javascript
174-
const viewer = document.querySelector('pdfjs-viewer-element')
175-
// Wait for the viewer initialization, receive PDFViewerApplication
176-
const viewerApp = await viewer.initialize()
177-
// Open PDF file data using Uint8Array instead of URL
178-
viewerApp.open({ data: pdfData })
138+
// Open PDF file programmatically accessing the viewer application
139+
document.addEventListener('DOMContentLoaded', async () => {
140+
document.querySelector('pdfjs-viewer-element').addEventListener('initialized', (event) => {
141+
const { viewerApp, viewerOptions } = event.detail
142+
viewerApp.open({ data: pdfData })
143+
})
144+
})
179145
```
180146

181-
## Accessiblity
147+
## Events
148+
149+
`initialized` - Fired after the PDF.js viewer is ready (after `PDFViewerApplication.initializedPromise` resolves). The event `detail` contains:
150+
151+
- `viewerApp` (`PDFViewerApplication`)
152+
- `viewerOptions` (`PDFViewerApplicationOptions`)
153+
154+
## Accessibility
182155

183156
Use `iframe-title` to add a title to the `iframe` element and improve accessibility.
184157

@@ -188,7 +161,7 @@ Use `iframe-title` to add a title to the `iframe` element and improve accessibil
188161

189162
Since v4 PDF.js requires `.mjs` files support, make sure your server has it.
190163

191-
In case of `nginx` this may causes to errors, see https://github.com/mozilla/pdf.js/issues/17296
164+
In case of `nginx` this may cause errors, see https://github.com/mozilla/pdf.js/issues/17296
192165

193166
Add `.mjs` files support for `nginx` example:
194167

@@ -209,13 +182,6 @@ server {
209182
}
210183
```
211184

212-
### Problem with range requests / streaming
213-
214-
Sometimes a PDF file fails to load when working with range requests / streaming.
215-
If you encounter this issue, you can try disabling the broken functionality of PDF.js:
216-
217-
`disable-range="true"`
218-
219185
## Support via Ko-fi
220186

221187
If you find `pdfjs-viewer-element` useful and want to support its development, consider making a donation via Ko-fi:

demo/basic.html

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>pdfjs-viewer-element | Basic demo</title>
7-
<script type="module" src="https://cdn.skypack.dev/pdfjs-viewer-element"></script>
7+
<script type="module" src="./../dist/pdfjs-viewer-element.js"></script>
88
<style>
99
body {
1010
margin: 0;
@@ -15,8 +15,7 @@
1515

1616
<body>
1717
<pdfjs-viewer-element
18-
src="/public/sample-pdf-with-images.pdf"
19-
viewer-path="/public/pdfjs-5.3.93-dist"
18+
src="/public/sample-pdf-with-images.pdf"
2019
style="height: 100dvh">
2120
</pdfjs-viewer-element>
2221
</body>

demo/dark-theme.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>pdfjs-viewer-element | Dark theme demo</title>
7-
<script type="module" src="https://cdn.skypack.dev/pdfjs-viewer-element"></script>
7+
<script type="module" src="../dist/pdfjs-viewer-element.js"></script>
88
<style>
99
body {
1010
margin: 0;
@@ -16,7 +16,6 @@
1616
<body>
1717
<pdfjs-viewer-element
1818
src="/public/sample-pdf-with-images.pdf"
19-
viewer-path="/public/pdfjs-5.3.93-dist"
2019
viewer-css-theme="DARK"
2120
style="height: 100dvh">
2221
</pdfjs-viewer-element>

demo/extra-styles-urls.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>pdfjs-viewer-element | Extra styles demo</title>
7-
<script type="module" src="https://cdn.skypack.dev/pdfjs-viewer-element"></script>
7+
<script type="module" src="../dist/pdfjs-viewer-element.js"></script>
88
<style>
99
body {
1010
margin: 0;
@@ -16,7 +16,6 @@
1616
<body>
1717
<pdfjs-viewer-element
1818
src="/public/sample-pdf-with-images.pdf"
19-
viewer-path="/public/pdfjs-5.3.93-dist"
2019
viewer-extra-styles-urls="['/demo/viewer-custom-theme.css']"
2120
style="height: 100dvh">
2221
</pdfjs-viewer-element>

demo/extra-styles.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>pdfjs-viewer-element | Extra styles demo</title>
7-
<script type="module" src="https://cdn.skypack.dev/pdfjs-viewer-element"></script>
7+
<script type="module" src="../dist/pdfjs-viewer-element.js"></script>
88
<style>
99
body {
1010
margin: 0;
@@ -16,7 +16,6 @@
1616
<body>
1717
<pdfjs-viewer-element
1818
src="/public/sample-pdf-with-images.pdf"
19-
viewer-path="/public/pdfjs-5.3.93-dist"
2019
viewer-extra-styles="#toolbarViewerMiddle { display: none; }"
2120
style="height: 100dvh">
2221
</pdfjs-viewer-element>

demo/go-to-nameddest.html

Lines changed: 0 additions & 24 deletions
This file was deleted.

demo/multiple.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>pdfjs-viewer-element | Multiple demo</title>
7+
<script type="module" src="./../dist/pdfjs-viewer-element.js"></script>
8+
<style>
9+
body {
10+
margin: 0;
11+
padding: 0;
12+
}
13+
</style>
14+
</head>
15+
16+
<body>
17+
<pdfjs-viewer-element
18+
src="/public/sample-pdf-with-images.pdf"
19+
viewer-css-theme="DARK"
20+
locale="de"
21+
style="height: 50dvh">
22+
</pdfjs-viewer-element>
23+
<pdfjs-viewer-element
24+
src="/public/sample-pdf-10MB.pdf"
25+
locale="pl"
26+
style="height: 50dvh">
27+
</pdfjs-viewer-element>
28+
</body>
29+
</html>

demo/opened-findbar.html

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>pdfjs-viewer-element | Opened find bar demo</title>
7-
<script type="module" src="https://cdn.skypack.dev/pdfjs-viewer-element"></script>
7+
<script type="module" src="../dist/pdfjs-viewer-element.js"></script>
88
<style>
99
body {
1010
margin: 0;
@@ -17,21 +17,19 @@
1717
<pdfjs-viewer-element
1818
id="viewer"
1919
src="/public/sample-pdf-with-images.pdf"
20-
viewer-path="/public/pdfjs-5.3.93-dist"
2120
style="height: 100dvh">
2221
</pdfjs-viewer-element>
2322

2423
<script>
2524
document.addEventListener('DOMContentLoaded', async () => {
26-
const viewer = document.querySelector('#viewer')
27-
// Wait for the viewer initialization
28-
const viewerApp = await viewer.initialize()
29-
// Wait for pages loaded, open find bar and search
30-
viewerApp.eventBus.on('pagesloaded', () => {
31-
viewerApp.findBar.open()
32-
viewerApp.findBar.findField.value = 'na'
33-
viewerApp.findBar.highlightAll.checked = true
34-
viewerApp.findBar.findNextButton.click()
25+
document.querySelector('#viewer').addEventListener('initialized', (event) => {
26+
const { viewerApp } = event.detail
27+
viewerApp.eventBus.on('pagesloaded', () => {
28+
viewerApp.findBar.open()
29+
viewerApp.findBar.findField.value = 'na'
30+
viewerApp.findBar.highlightAll.checked = true
31+
viewerApp.findBar.findNextButton.click()
32+
})
3533
})
3634
})
3735
</script>

demo/pdf-data-load.html

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<meta charset="UTF-8" />
55
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
66
<title>pdfjs-viewer-element | Basic demo</title>
7-
<script type="module" src="https://cdn.skypack.dev/pdfjs-viewer-element"></script>
7+
<script type="module" src="../dist/pdfjs-viewer-element.js"></script>
88
<style>
99
body {
1010
margin: 0;
@@ -16,7 +16,6 @@
1616
<body>
1717
<pdfjs-viewer-element
1818
id="viewer"
19-
viewer-path="/public/pdfjs-5.3.93-dist"
2019
style="height: 100dvh">
2120
</pdfjs-viewer-element>
2221

@@ -38,11 +37,10 @@
3837

3938

4039
document.addEventListener('DOMContentLoaded', async () => {
41-
const viewer = document.querySelector('#viewer')
42-
// Wait for the viewer initialization
43-
const viewerApp = await viewer.initialize()
44-
// Open PDF file data using Uint8Array instead of URL
45-
viewerApp.open({ data: pdfData })
40+
document.querySelector('#viewer').addEventListener('initialized', (event) => {
41+
const { viewerApp } = event.detail
42+
viewerApp.open({ data: pdfData })
43+
})
4644
})
4745
</script>
4846
</body>

0 commit comments

Comments
 (0)