Skip to content

Commit 09ca1d1

Browse files
authored
Merge pull request #45 from alekswebnet/3.1.2
3.1.2
2 parents a3f56c0 + ba16de2 commit 09ca1d1

File tree

8 files changed

+112
-9
lines changed

8 files changed

+112
-9
lines changed

README.md

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

1515
## Features
1616

17-
- Standalone web component with no runtime dependencies
17+
- Standalone isolated web component with no runtime dependencies
1818
- Drop-in, iframe-based PDF.js default viewer for any web app
1919
- Works with same-origin and cross-origin PDF documents
2020
- Configure via attributes and URL parameters (page, zoom, search, pagemode, locale)
@@ -86,8 +86,9 @@ The element is block-level and needs an explicit height.
8686
| `zoom` | Zoom level (for example `auto`, `page-width`, `200%`). | `''` |
8787
| `pagemode` | Sidebar mode: `thumbs`, `bookmarks`, `attachments`, `layers`, `none`. | `none` |
8888
| `locale` | Viewer UI locale (for example `en-US`, `de`, `uk`). [Available locales](https://github.com/mozilla/pdf.js/tree/master/l10n) | `''` |
89+
| `locale-src-template` | Locale file URL template. Must contain `{locale}` placeholder. Used together with `locale`. | `https://cdn.jsdelivr.net/gh/mozilla-l10n/firefox-l10n@main/{locale}/toolkit/toolkit/pdfviewer/viewer.ftl` |
8990
| `viewer-css-theme` | Viewer theme: `AUTOMATIC`, `LIGHT`, `DARK`. | `AUTOMATIC` |
90-
| `worker-src` | PDF.js worker URL override. | `https://cdn.jsdelivr.net/npm/pdfjs-dist@5.4.624/build/pdf.worker.min.mjs` |
91+
| `worker-src` | PDF.js worker URL override. | `<package-url>/pdf.worker.min.mjs` |
9192

9293
Play with attributes on [API docs page](https://alekswebnet.github.io/pdfjs-viewer-element/#api).
9394

@@ -101,6 +102,41 @@ Most attributes can be updated dynamically:
101102
- `worker-src` updates viewer options for subsequent document loads.
102103
- `locale` rebuilds the viewer so localization resources can be applied.
103104

105+
## Worker source
106+
107+
By default, the component resolves `worker-src` to the worker shipped with this package (`pdf.worker.min.mjs` in `dist`).
108+
109+
Set `worker-src` only if you want to serve the worker from a custom location (for example your own CDN or static assets path).
110+
111+
- The URL must point to a valid PDF.js module worker file.
112+
- The worker version should match the bundled PDF.js version.
113+
114+
```html
115+
<pdfjs-viewer-element
116+
src="/file.pdf"
117+
worker-src="https://cdn.jsdelivr.net/npm/pdfjs-dist@5.5.207/build/pdf.worker.min.mjs">
118+
</pdfjs-viewer-element>
119+
```
120+
121+
## Locale source template
122+
123+
Use `locale-src-template` when you need to load localization files from a custom host.
124+
125+
- The template must include `{locale}`.
126+
- `{locale}` is replaced by the `locale` attribute value (for example `de`, `uk`, `en-US`).
127+
- If `locale` is not set, no locale file is loaded.
128+
- Changes to `locale-src-template` are applied when the viewer is (re)initialized, for example after setting/changing `locale`.
129+
130+
Example:
131+
132+
```html
133+
<pdfjs-viewer-element
134+
src="/file.pdf"
135+
locale="de"
136+
locale-src-template="https://cdn.example.com/pdfjs-locales/{locale}/viewer.ftl">
137+
</pdfjs-viewer-element>
138+
```
139+
104140
## Viewer CSS theme
105141

106142
Use `viewer-css-theme` attribute to set light or dark theme manually:
@@ -213,7 +249,7 @@ You can also react to source changes dynamically:
213249

214250
```javascript
215251
const viewerElement = document.querySelector('pdfjs-viewer-element')
216-
viewer.setAttribute('src', '/another-file.pdf')
252+
viewerElement.setAttribute('src', '/another-file.pdf')
217253
```
218254

219255
## Accessibility

demo/custom-worker-src.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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 | Custom Worker Src 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+
worker-src="https://cdn.jsdelivr.net/npm/pdfjs-dist@v5.5.207/build/pdf.worker.min.mjs"
20+
style="height: 100dvh">
21+
</pdfjs-viewer-element>
22+
</body>
23+
</html>

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pdfjs-viewer-element",
3-
"version": "3.1.1",
3+
"version": "3.1.2",
44
"license": "MIT",
55
"author": {
66
"name": "Oleksandr Shevchuk",
@@ -37,7 +37,7 @@
3737
],
3838
"scripts": {
3939
"dev": "vite",
40-
"build": "tsc && vite build",
40+
"build": "tsc && vite build && node scripts/copy-worker.mjs",
4141
"test": "vitest",
4242
"coverage": "vitest run --coverage"
4343
},
@@ -47,6 +47,7 @@
4747
"@vitest/browser": "^4.0.18",
4848
"@vitest/browser-webdriverio": "^4.0.18",
4949
"jsdom": "^28.1.0",
50+
"terser": "^5.39.0",
5051
"typescript": "^5.9.3",
5152
"vite": "^7.3.1",
5253
"vitest": "^4.0.18",

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

scripts/copy-worker.mjs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { mkdir, readFile, writeFile } from 'node:fs/promises'
2+
import { resolve } from 'node:path'
3+
import { minify } from 'terser'
4+
5+
const rootDir = resolve(import.meta.dirname, '..')
6+
const sourceFile = resolve(rootDir, 'src', 'build', 'pdf.worker.mjs')
7+
const distDir = resolve(rootDir, 'dist')
8+
const targetFile = resolve(distDir, 'pdf.worker.min.mjs')
9+
10+
await mkdir(distDir, { recursive: true })
11+
12+
const workerCode = await readFile(sourceFile, 'utf8')
13+
const result = await minify(workerCode, {
14+
module: true,
15+
compress: true,
16+
mangle: true,
17+
format: {
18+
comments: false
19+
}
20+
})
21+
22+
if (!result.code) {
23+
throw new Error('Worker minification failed: empty output')
24+
}
25+
26+
await writeFile(targetFile, result.code, 'utf8')

src/pdfjs-viewer-element.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const PDFJS_VERSION = '5.5.207'
1+
const DEFAULT_WORKER_SRC = import.meta.env.DEV
2+
? new URL('./build/pdf.worker.mjs', import.meta.url).href
3+
: new URL('./pdf.worker.min.mjs', import.meta.url).href
24

35
const DEFAULTS = {
46
src: '',
@@ -10,7 +12,8 @@ const DEFAULTS = {
1012
pagemode: 'none',
1113
locale: '',
1214
viewerCssTheme: 'AUTOMATIC',
13-
workerSrc: `https://cdn.jsdelivr.net/npm/pdfjs-dist@${PDFJS_VERSION}/build/pdf.worker.min.mjs`
15+
workerSrc: DEFAULT_WORKER_SRC,
16+
localeSrcTemplate: 'https://cdn.jsdelivr.net/gh/mozilla-l10n/firefox-l10n@main/{locale}/toolkit/toolkit/pdfviewer/viewer.ftl'
1417
} as const
1518

1619
export const ViewerCssTheme = { AUTOMATIC: 0, LIGHT: 1, DARK: 2 } as const
@@ -38,6 +41,13 @@ export class PdfjsViewerElement extends HTMLElement {
3841
]
3942
}
4043

44+
private formatTemplate(template: string, params: Record<string, any>) {
45+
return template.replace(/\{(\w+)\}/g, (_, key) => {
46+
if (!(key in params)) throw new Error(`Missing param: ${key}`);
47+
return String(params[key]);
48+
});
49+
}
50+
4151
private getFullPath(path: string) {
4252
return path.startsWith('/') ? `${window.location.origin}${path}` : path
4353
}
@@ -120,8 +130,12 @@ export class PdfjsViewerElement extends HTMLElement {
120130
this.cleanupLocaleResource()
121131
return
122132
}
133+
const localeUrl = this.formatTemplate(
134+
this.getAttribute('locale-src-template') || DEFAULTS.localeSrcTemplate,
135+
{ locale }
136+
)
123137
const localeObject = {
124-
[String(locale)]: `https://raw.githubusercontent.com/mozilla-l10n/firefox-l10n/main/${locale}/toolkit/toolkit/pdfviewer/viewer.ftl`
138+
[String(locale)]: localeUrl
125139
}
126140
const localeLink = doc.createElement('link')
127141
localeLink.rel = 'resource'

types/pdfjs-viewer-element.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export declare class PdfjsViewerElement extends HTMLElement {
1111
private localeResourceLink?;
1212
private viewerStyles;
1313
static get observedAttributes(): string[];
14+
private formatTemplate;
1415
private getFullPath;
1516
private getCssThemeOption;
1617
private applyIframeHash;

vite.config.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export default defineConfig({
1111
output: {
1212
plugins: [
1313
terser({
14-
'sourceMap': false,
1514
compress: true,
1615
mangle: false,
1716
format: { comments: false }

0 commit comments

Comments
 (0)