@zinejs/pdf 0.8.2 → 0.9.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,73 @@
1
+ # PolyForm Noncommercial License 1.0.0
2
+
3
+ <https://polyformproject.org/licenses/noncommercial/1.0.0>
4
+
5
+ ## Acceptance
6
+
7
+ In order to get any license under these terms, you must agree to them as both strict obligations and conditions to all your licenses.
8
+
9
+ ## Copyright License
10
+
11
+ The licensor grants you a copyright license for the software to do everything you might do with the software that would otherwise infringe the licensor's copyright in it for any permitted purpose. However, you may only distribute the software according to [Distribution License](#distribution-license) and make changes or new works based on the software according to [Changes and New Works License](#changes-and-new-works-license).
12
+
13
+ ## Distribution License
14
+
15
+ The licensor grants you an additional copyright license to distribute copies of the software. Your license to distribute covers distributing the software with changes and new works permitted by [Changes and New Works License](#changes-and-new-works-license).
16
+
17
+ ## Notices
18
+
19
+ You must ensure that anyone who gets a copy of any part of the software from you also gets a copy of these terms or the URL for them above, as well as copies of any plain-text lines beginning with `Required Notice:` that the licensor provided with the software. For example:
20
+
21
+ > Required Notice: Copyright Yoyodyne, Inc. (http://example.com)
22
+
23
+ ## Changes and New Works License
24
+
25
+ The licensor grants you an additional copyright license to make changes and new works based on the software for any permitted purpose.
26
+
27
+ ## Patent License
28
+
29
+ The licensor grants you a patent license for the software that covers patent claims the licensor can license, or becomes able to license, that you would infringe by using the software.
30
+
31
+ ## Noncommercial Purposes
32
+
33
+ Any noncommercial purpose is a permitted purpose.
34
+
35
+ ## Personal Uses
36
+
37
+ Personal use for research, experiment, and testing for the benefit of public knowledge, personal study, private entertainment, hobby projects, amateur pursuits, or religious observance, without any anticipated commercial application, is use for a permitted purpose.
38
+
39
+ ## Noncommercial Organizations
40
+
41
+ Use by any charitable organization, educational institution, public research organization, public safety or health organization, environmental protection organization, or government institution is use for a permitted purpose regardless of the source of funding or obligations resulting from the funding.
42
+
43
+ ## Fair Use
44
+
45
+ You may have "fair use" rights for the software under the law. These terms do not limit them.
46
+
47
+ ## No Other Rights
48
+
49
+ These terms do not allow you to sublicense or transfer any of your licenses to anyone else, or prevent the licensor from granting licenses to anyone else. These terms do not imply any other licenses.
50
+
51
+ ## Patent Defense
52
+
53
+ If you make any written claim that the software infringes or contributes to infringement of any patent, your patent license for the software granted under these terms ends immediately. If your company makes such a claim, your patent license ends immediately for work on behalf of your company.
54
+
55
+ ## Violations
56
+
57
+ The first time you are notified in writing that you have violated any of these terms, or done anything with the software not covered by your licenses, your licenses can nonetheless continue if you come into full compliance with these terms, and take practical steps to correct past violations, within 32 days of receiving notice. Otherwise, all your licenses end immediately.
58
+
59
+ ## No Liability
60
+
61
+ ***As far as the law allows, the software comes as is, without any warranty or condition, and the licensor will not be liable to you for any damages arising out of these terms or the use or nature of the software, under any kind of legal claim.***
62
+
63
+ ## Definitions
64
+
65
+ The **licensor** is the individual or entity offering these terms, and the **software** is the software the licensor makes available under these terms.
66
+
67
+ **You** refers to the individual or entity agreeing to these terms.
68
+
69
+ **Your company** is any legal entity, sole proprietorship, or other kind of organization that you work for, plus all organizations that have control over, are under the control of, or are under common control with that organization. **Control** means ownership of substantially all the assets of an entity, or the power to direct its management and policies by vote, contract, or otherwise. Control can be direct or indirect.
70
+
71
+ **Your licenses** are all the licenses granted to you for the software under these terms.
72
+
73
+ **Use** means anything you do with the software requiring one of your licenses.
package/README.md CHANGED
@@ -1,6 +1,8 @@
1
1
  # @zinejs/pdf
2
2
 
3
- PDF content source for [zinejs](https://github.com/), powered by [pdf.js](https://mozilla.github.io/pdf.js/). Renders PDF pages to canvases that the zinejs core paints as a flipbook.
3
+ PDF content source for [zinejs](https://zinejs.com/docs/), powered by [pdf.js](https://mozilla.github.io/pdf.js/). Renders PDF pages to canvases that [`@zinejs/core`](https://www.npmjs.com/package/@zinejs/core) paints as a flipbook.
4
+
5
+ > **Docs and examples:** [zinejs.com/docs](https://zinejs.com/docs/) · [CDN example](https://zinejs.com/examples/html-cdn)
4
6
 
5
7
  ## Install
6
8
 
@@ -8,118 +10,84 @@ PDF content source for [zinejs](https://github.com/), powered by [pdf.js](https:
8
10
  npm install @zinejs/core @zinejs/pdf
9
11
  ```
10
12
 
11
- `pdfjs-dist` is a normal dependency of `@zinejs/pdf`, so it installs with the package. The plugin still loads it lazily (dynamic `import`) so it never lands in the `@zinejs/core` bundle. If your app already depends on `pdfjs-dist`, the package manager will typically dedupe to one copy — keep major versions compatible so the worker matches the library.
13
+ `pdfjs-dist` installs with this package. It is loaded lazily (dynamic `import`) so it never lands in the `@zinejs/core` bundle. If your app already depends on `pdfjs-dist`, keep major versions compatible so the worker matches the library.
12
14
 
13
15
  ## Usage
14
16
 
15
17
  ```js
16
- import { Zine } from '@zinejs/core';
17
- import { PdfSource } from '@zinejs/pdf';
18
+ import { Zine } from '@zinejs/core'
19
+ import { PdfSource } from '@zinejs/pdf'
18
20
 
19
- const book = new Zine(document.getElementById('book'), {
21
+ new Zine(document.getElementById('book'), {
20
22
  source: new PdfSource('document.pdf'),
21
- });
22
- ```
23
-
24
- Under a bundler that's all you need — the pdf.js worker is resolved for you.
25
-
26
- ### CDN / no bundler
27
-
28
- Both packages ship **UMD** builds (`dist/index.umd.js`) that share the `ZineJS` global — load **core first**, then pdf (the pdf build uses `extend: true` so it merges into `ZineJS` instead of replacing it).
29
-
30
- Modern `pdfjs-dist` is ESM-only, so load it in a module script first, expose
31
- `globalThis.pdfjsLib`, then use classic deferred `<script>` tags for the UMD
32
- builds (and pass an explicit `workerSrc` — no bundler rewrites the worker URL):
33
-
34
- ```html
35
- <script type="module">
36
- import * as pdfjsLib from 'https://cdn.jsdelivr.net/npm/pdfjs-dist/build/pdf.min.mjs';
37
- globalThis.pdfjsLib = pdfjsLib;
38
- </script>
39
-
40
- <script defer src="https://cdn.jsdelivr.net/npm/@zinejs/core/dist/index.umd.js"></script>
41
- <script defer src="https://cdn.jsdelivr.net/npm/@zinejs/pdf/dist/index.umd.js"></script>
42
- <script defer>
43
- const book = new ZineJS.Zine(document.getElementById('book'), {
44
- source: new ZineJS.PdfSource('document.pdf', {
45
- workerSrc: 'https://cdn.jsdelivr.net/npm/pdfjs-dist/build/pdf.worker.min.mjs',
46
- }),
47
- });
48
- </script>
49
- ```
50
-
51
- Image-only books can skip pdf.js and use a plain classic script:
52
-
53
- ```html
54
- <script src="https://cdn.jsdelivr.net/npm/@zinejs/core/dist/index.umd.js"></script>
55
- <script>
56
- new ZineJS.Zine(document.getElementById('book'), {
57
- source: new ZineJS.ImageSource(['page-01.png', 'page-02.png']),
58
- });
59
- </script>
23
+ })
60
24
  ```
61
25
 
62
- `PdfSource` prefers `globalThis.pdfjsLib` when present (CDN), otherwise dynamic-imports `pdfjs-dist` (bundlers).
63
-
64
- ## The pdf.js worker (`workerSrc`)
26
+ Under a bundler that is all you need: the pdf.js worker is resolved for you.
65
27
 
66
- pdf.js parses and rasterizes in a Web Worker, a separate file the host must locate. By default `PdfSource` resolves it to (the `legacy/` path since `legacy` defaults to true; `build/` when `legacy: false`):
28
+ `src` can also be raw bytes or a pdf.js document you already opened:
67
29
 
68
30
  ```js
69
- new URL('pdfjs-dist/legacy/build/pdf.worker.min.mjs', import.meta.url).href
31
+ new PdfSource(arrayBuffer)
32
+ new PdfSource(await getDocument(url).promise)
70
33
  ```
71
34
 
72
- Resolution precedence: an explicit `workerSrc` option → an already-set `pdfjsLib.GlobalWorkerOptions.workerSrc` → the auto-default above. You only pass `workerSrc` when the default can't apply.
35
+ ## The pdf.js worker
73
36
 
74
- ### Vite / webpack 5 / esbuild
37
+ pdf.js parses and rasterizes in a Web Worker. By default `PdfSource` finds the worker via Vite's
38
+ `?url` import, webpack's `new URL('pdfjs-dist/…', import.meta.url)` emit, or the sibling
39
+ `pdfjs-dist` install. If that cannot be resolved, it falls back to a jsDelivr URL pinned to the
40
+ same `pdfjs-dist` version that loaded, and warns once in the console. Set `cdnFallback: false`
41
+ for CSP-restricted or offline apps (then pass `workerSrc`, or the open fails clearly instead of
42
+ loading a network worker that CSP would block).
75
43
 
76
- Nothing to do — these bundlers statically rewrite the `new URL(..., import.meta.url)` above and emit the worker asset:
44
+ Resolution order: `workerSrc` option, then `pdfjsLib.GlobalWorkerOptions.workerSrc`, then that
45
+ auto-default. Pass `workerSrc` for CDN / UMD, offline / CSP setups, or if auto-resolve fails in
46
+ your bundler:
77
47
 
78
48
  ```js
79
- new PdfSource('document.pdf');
80
- ```
81
-
82
- ### Custom path
83
-
84
- Serving the worker yourself (copied to your public dir, etc.):
49
+ import { PdfSource } from '@zinejs/pdf'
50
+ import workerSrc from 'pdfjs-dist/legacy/build/pdf.worker.min.mjs?url'
85
51
 
86
- ```js
87
- new PdfSource('document.pdf', { workerSrc: '/assets/pdf.worker.min.mjs' });
52
+ new PdfSource('document.pdf', { workerSrc })
53
+ // CSP / offline: also disable the CDN last resort
54
+ new PdfSource('document.pdf', { workerSrc, cdnFallback: false })
88
55
  ```
89
56
 
90
- ### Already configuring pdf.js yourself
91
-
92
- If your app sets `GlobalWorkerOptions.workerSrc`, `PdfSource` leaves it alone — no `workerSrc` needed.
57
+ Keep the worker on the same build as the library (`legacy` vs modern). If you already set
58
+ `GlobalWorkerOptions.workerSrc`, leave `workerSrc` off. A pre-created pdf.js document needs no
59
+ worker from this package.
93
60
 
94
- ## Passing bytes or a pre-created document
95
-
96
- ```js
97
- new PdfSource(arrayBuffer); // raw PDF bytes (ArrayBuffer | Uint8Array)
98
- new PdfSource(await getDocument(...).promise); // a pdf.js document you created; no workerSrc needed
99
- ```
61
+ CDN / no bundler: load pdf.js first, expose `globalThis.pdfjsLib`, then the UMD builds (core
62
+ first). See the [HTML CDN example](https://zinejs.com/examples/html-cdn). A UMD host has no
63
+ bundler to resolve the worker locally, so it uses your `workerSrc`, an already-set
64
+ `GlobalWorkerOptions.workerSrc`, or the version-matched CDN fallback (matched to the global
65
+ pdf.js's version; silent here, since a CSP/offline failure surfaces through the browser and pdf.js
66
+ anyway). Set `cdnFallback: false` to require explicit configuration instead.
100
67
 
101
68
  ## Options
102
69
 
103
- ```ts
70
+ ```js
104
71
  new PdfSource(src, {
105
- workerSrc, // string — override the auto-resolved worker URL (see above)
72
+ workerSrc, // override the auto-resolved worker URL
73
+ cdnFallback: true, // false: never load the worker from jsDelivr (CSP / offline)
106
74
  renderScale: 1, // pages rasterize at renderScale × devicePixelRatio
107
- preload: 1, // adjacent pages to prefetch around a requested page
108
- maxCacheBytes, // soft cap on cached page bytes (default ~256 MB); LRU-evicts beyond it
109
- progressive: true, // paint a low-res page first, then swap to crisp (faster first paint)
110
- disableAutoFetch: true, // fetch only the byte ranges visible pages need (range-capable servers)
111
- legacy: true, // load pdf.js's transpiled build (default); false serves the lean modern build
112
- });
75
+ preload: 1, // adjacent pages to prefetch (default 1)
76
+ maxCacheBytes, // soft cap on cached page bytes (default ~256 MB)
77
+ progressive: false, // true: low-res first, then crisp
78
+ disableAutoFetch: false, // true: fetch only the ranges visible pages need
79
+ legacy: true, // false: lean modern pdf.js (recent engines only)
80
+ })
113
81
  ```
114
82
 
115
- `src` is a URL string, `ArrayBuffer`/`Uint8Array` of PDF bytes, or a pre-created pdf.js document.
83
+ ### Legacy vs modern (`legacy`)
116
84
 
117
- ### Legacy vs modern build (`legacy`)
118
-
119
- pdf.js ships two builds. The `legacy` build is transpiled with polyfills and runs on both older and current browsers; the `modern` build is smaller and a touch faster but needs a recent engine. `PdfSource` defaults to `legacy: true`, favouring reach. Pass `legacy: false` to load the modern build when your audience is on current browsers:
85
+ pdf.js ships two builds. The **legacy** build is transpiled with polyfills and is the default, so PDF books stay usable on older engines as well as current ones. The **modern** build is smaller and a touch faster. Pass `legacy: false` when your audience is on recent browsers:
120
86
 
121
87
  ```js
122
- new PdfSource('document.pdf', { legacy: false });
88
+ new PdfSource('document.pdf', { legacy: false })
123
89
  ```
124
90
 
125
- The flag also picks the matching worker (`legacy/build/pdf.worker.min.mjs` vs `build/pdf.worker.min.mjs`) — the two must be a matched pair. It's ignored when you pass a pre-created document or set `globalThis.pdfjsLib`, since those already chose their build.
91
+ The flag also picks the matching worker. It is ignored when you pass a pre-created document or set `globalThis.pdfjsLib`, since those already chose their build.
92
+
93
+ Mozilla documents legacy as Chrome 125+, Firefox ESR, Safari 18+, and Chromium Edge. We have seen PDF text rasterize correctly back to Chrome 114. Full notes: [browser support](https://github.com/awecode/zinejs#browser-support).