@chalidade/protect-pdf 0.0.0-stage → 0.1.0
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 +21 -0
- package/README.md +97 -2
- package/THIRD-PARTY-NOTICES.md +18 -0
- package/dist/chunks/protect-Dz2WFVuy.js +87 -0
- package/dist/chunks/qpdf-B0sSsX-b.js +2278 -0
- package/dist/chunks/qpdf-Bycd2-RO.js +3 -0
- package/dist/element.d.ts +16 -0
- package/dist/element.js +10789 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +1 -0
- package/package.json +45 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Chalid Ade Rahman
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,98 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @chalidade/protect-pdf
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Lock a PDF with a password on any website: one `<script>` tag, one element.
|
|
4
|
+
The PDF is encrypted with **AES-256** (PDF 2.0 security handler, revision 6)
|
|
5
|
+
and can forbid printing, copying text or editing. **Everything runs in the
|
|
6
|
+
visitor's browser** with qpdf compiled to WebAssembly; neither the file nor
|
|
7
|
+
the password leaves the device, and the password is not stored.
|
|
8
|
+
|
|
9
|
+
This is the [Protect PDF](https://chalidade.github.io/tools/#/protect-pdf) tool from
|
|
10
|
+
[chalidade/tools](https://github.com/chalidade/tools), packaged so you can drop
|
|
11
|
+
it into your own site. Try it there first.
|
|
12
|
+
|
|
13
|
+
## Install in 10 seconds
|
|
14
|
+
|
|
15
|
+
Paste this into any HTML page (WordPress, Blogger, Webflow, plain HTML):
|
|
16
|
+
|
|
17
|
+
```html
|
|
18
|
+
<script type="module" src="https://cdn.jsdelivr.net/npm/@chalidade/protect-pdf@0/dist/element.js"></script>
|
|
19
|
+
|
|
20
|
+
<protect-pdf></protect-pdf>
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
`element.js` is 96.10 kB gzipped (React and styles included). The
|
|
24
|
+
heavy libraries are separate files that load only when a visitor uses the
|
|
25
|
+
tool, from the same place as `element.js` (gzipped: qpdf (WebAssembly) ~619 kB).
|
|
26
|
+
The permissions are locked with a random owner password that nobody gets to
|
|
27
|
+
see, so the open password alone cannot lift them in readers that honour them.
|
|
28
|
+
Adobe Reader, browsers and most PDF apps do; some do not.
|
|
29
|
+
|
|
30
|
+
### Attributes
|
|
31
|
+
|
|
32
|
+
| Attribute | Value | Default |
|
|
33
|
+
| --- | --- | --- |
|
|
34
|
+
| `theme` | `auto` (follows the system), `light`, `dark` | `auto` |
|
|
35
|
+
| `lang` | `en` or `id`, the UI language | the visitor's browser language |
|
|
36
|
+
|
|
37
|
+
### Colors
|
|
38
|
+
|
|
39
|
+
The element uses Shadow DOM, so your site's CSS can't break it (and vice versa).
|
|
40
|
+
Change its accent colors with CSS variables:
|
|
41
|
+
|
|
42
|
+
```css
|
|
43
|
+
protect-pdf {
|
|
44
|
+
--sr-brand-1: #0ea5e9; /* accent gradient, light and dark */
|
|
45
|
+
--sr-brand-2: #6366f1;
|
|
46
|
+
--radius: 0.5rem; /* corner radius */
|
|
47
|
+
}
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Content Security Policy
|
|
51
|
+
|
|
52
|
+
The PDF engines are bundled inside the package as `data:` URLs (no request to
|
|
53
|
+
any other server).
|
|
54
|
+
On a page with a strict CSP, the PDF engines run under this policy (tested in Chrome); add the CDN
|
|
55
|
+
you load `element.js` from to `script-src`:
|
|
56
|
+
|
|
57
|
+
```
|
|
58
|
+
default-src 'self'; script-src 'self' data: 'wasm-unsafe-eval'; worker-src blob:;
|
|
59
|
+
connect-src 'self' data:; img-src 'self' data: blob:; style-src 'self' 'unsafe-inline'
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Use with npm
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
npm install @chalidade/protect-pdf
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
```js
|
|
69
|
+
import '@chalidade/protect-pdf/element' // registers <protect-pdf>
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
In React, `<protect-pdf theme="light" />` works directly in JSX (React 19
|
|
73
|
+
passes attributes through to custom elements).
|
|
74
|
+
|
|
75
|
+
## Headless API
|
|
76
|
+
|
|
77
|
+
No UI: plain async functions for building your own interface. Browser only
|
|
78
|
+
(they use `File`, `Blob` and canvas).
|
|
79
|
+
|
|
80
|
+
```js
|
|
81
|
+
import { encryptionInfo, protectPdf } from '@chalidade/protect-pdf'
|
|
82
|
+
|
|
83
|
+
const bytes = new Uint8Array(await file.arrayBuffer())
|
|
84
|
+
const locked = await protectPdf(bytes, 'correct horse', { print: true, copy: false, modify: false })
|
|
85
|
+
await encryptionInfo(locked) // { encrypted: true, needsPassword: true }
|
|
86
|
+
|
|
87
|
+
// Already encrypted? protectPdf throws AlreadyEncryptedError.
|
|
88
|
+
await protectPdf(locked, 'x', { print: true, copy: true, modify: true }) // throws AlreadyEncryptedError
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
`pdfinfo` (poppler) on the result: `Encrypted: yes (print:yes copy:no change:no
|
|
92
|
+
addNotes:no algorithm:AES-256)`. The element asks for at least 4 characters;
|
|
93
|
+
`protectPdf` only refuses an empty password, so enforce your own policy.
|
|
94
|
+
|
|
95
|
+
## License
|
|
96
|
+
|
|
97
|
+
MIT © Chalid Ade Rahman. The element bundles third-party libraries, see
|
|
98
|
+
[THIRD-PARTY-NOTICES.md](./THIRD-PARTY-NOTICES.md).
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# Third-party notices
|
|
2
|
+
|
|
3
|
+
The files in `dist/` bundle the following open-source software, unmodified.
|
|
4
|
+
|
|
5
|
+
| Package | License | Source |
|
|
6
|
+
| --- | --- | --- |
|
|
7
|
+
| React, React DOM | MIT | https://github.com/facebook/react |
|
|
8
|
+
| lucide-react | ISC | https://github.com/lucide-icons/lucide |
|
|
9
|
+
| Tailwind CSS (compiled styles) | MIT | https://github.com/tailwindlabs/tailwindcss |
|
|
10
|
+
| tw-animate-css | MIT | https://github.com/Wombosvideo/tw-animate-css |
|
|
11
|
+
| clsx | MIT | https://github.com/lukeed/clsx |
|
|
12
|
+
| tailwind-merge | MIT | https://github.com/dcastil/tailwind-merge |
|
|
13
|
+
| class-variance-authority | Apache-2.0 | https://github.com/joe-bell/cva |
|
|
14
|
+
| qpdf, compiled to WebAssembly by @neslinesli93/qpdf-wasm | Apache-2.0 (qpdf), ISC (qpdf-wasm build) | https://github.com/qpdf/qpdf, https://github.com/neslinesli93/qpdf-wasm |
|
|
15
|
+
| libjpeg-turbo (inside the qpdf WebAssembly) | IJG, BSD-3-Clause, Zlib | https://github.com/libjpeg-turbo/libjpeg-turbo |
|
|
16
|
+
| zlib (inside the qpdf WebAssembly) | Zlib | https://zlib.net |
|
|
17
|
+
|
|
18
|
+
The full license texts are in each project's repository.
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// \0rolldown/runtime.js
|
|
2
|
+
var e = Object.create, t = Object.defineProperty, n = Object.getOwnPropertyDescriptor, r = Object.getOwnPropertyNames, i = Object.getPrototypeOf, a = Object.prototype.hasOwnProperty, o = (e, t) => () => (t || (e((t = { exports: {} }).exports, t, t.exports), e = null), t.exports), s = (e, i, o, s) => {
|
|
3
|
+
if (i && typeof i == "object" || typeof i == "function") for (var c = r(i), l = 0, u = c.length, d; l < u; l++) d = c[l], !a.call(e, d) && d !== o && t(e, d, {
|
|
4
|
+
get: ((e) => i[e]).bind(null, d),
|
|
5
|
+
enumerable: !(s = n(i, d)) || s.enumerable
|
|
6
|
+
});
|
|
7
|
+
return e;
|
|
8
|
+
}, c = (n, r, o) => (o = n == null ? {} : e(i(n)), s(r || !n || !n.__esModule || !a.call(n, "default") ? t(o, "default", {
|
|
9
|
+
value: n,
|
|
10
|
+
enumerable: !0
|
|
11
|
+
}) : o, n)), l = "/in", u = "/out";
|
|
12
|
+
async function d(e, t) {
|
|
13
|
+
let [{ default: n }, { default: r }] = await Promise.all([import("./qpdf-B0sSsX-b.js").then((e) => /* @__PURE__ */ c(e.default, 1)), import("./qpdf-Bycd2-RO.js")]), i = null, a = {
|
|
14
|
+
log: console.log,
|
|
15
|
+
error: console.error
|
|
16
|
+
}, o = (e) => (...t) => i ? i.push(t.map(String).join(" ")) : e(...t);
|
|
17
|
+
console.log = o(a.log), console.error = o(a.error);
|
|
18
|
+
let s;
|
|
19
|
+
try {
|
|
20
|
+
s = n({
|
|
21
|
+
locateFile: () => r,
|
|
22
|
+
noInitialRun: !0
|
|
23
|
+
});
|
|
24
|
+
} finally {
|
|
25
|
+
Object.assign(console, a);
|
|
26
|
+
}
|
|
27
|
+
let d = await s;
|
|
28
|
+
d.FS.writeFile(l, e);
|
|
29
|
+
let f = [];
|
|
30
|
+
i = f;
|
|
31
|
+
let p;
|
|
32
|
+
try {
|
|
33
|
+
p = d.callMain(t(l, u));
|
|
34
|
+
} catch (e) {
|
|
35
|
+
p = typeof e.status == "number" ? e.status : 2;
|
|
36
|
+
} finally {
|
|
37
|
+
i = null;
|
|
38
|
+
}
|
|
39
|
+
let m;
|
|
40
|
+
try {
|
|
41
|
+
m = d.FS.readFile(u);
|
|
42
|
+
} catch {}
|
|
43
|
+
return {
|
|
44
|
+
code: p,
|
|
45
|
+
log: f.join("\n"),
|
|
46
|
+
output: m
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
var f = (e) => /invalid password/i.test(e);
|
|
50
|
+
async function p(e) {
|
|
51
|
+
let { code: t, log: n } = await d(e, (e) => ["--show-encryption", e]);
|
|
52
|
+
return f(n) ? {
|
|
53
|
+
encrypted: !0,
|
|
54
|
+
needsPassword: !0
|
|
55
|
+
} : t !== 2 && /not encrypted/i.test(n) ? {
|
|
56
|
+
encrypted: !1,
|
|
57
|
+
needsPassword: !1
|
|
58
|
+
} : {
|
|
59
|
+
encrypted: t !== 2,
|
|
60
|
+
needsPassword: !1
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
// ../../src/tools/protect-pdf/protect.ts
|
|
64
|
+
var m = class extends Error {};
|
|
65
|
+
function h() {
|
|
66
|
+
let e = crypto.getRandomValues(/* @__PURE__ */ new Uint8Array(24));
|
|
67
|
+
return Array.from(e, (e) => e.toString(36).padStart(2, "0")).join("").slice(0, 32);
|
|
68
|
+
}
|
|
69
|
+
async function g(e, t, n) {
|
|
70
|
+
if (!t) throw Error("password required");
|
|
71
|
+
if ((await p(e)).encrypted) throw new m();
|
|
72
|
+
let { code: r, output: i } = await d(e, (e, r) => [
|
|
73
|
+
e,
|
|
74
|
+
"--encrypt",
|
|
75
|
+
`--user-password=${t}`,
|
|
76
|
+
`--owner-password=${h()}`,
|
|
77
|
+
"--bits=256",
|
|
78
|
+
`--print=${n.print ? "full" : "none"}`,
|
|
79
|
+
`--extract=${n.copy ? "y" : "n"}`,
|
|
80
|
+
`--modify=${n.modify ? "all" : "none"}`,
|
|
81
|
+
"--",
|
|
82
|
+
r
|
|
83
|
+
]);
|
|
84
|
+
if (r === 2 || !i) throw Error("qpdf");
|
|
85
|
+
return i;
|
|
86
|
+
}
|
|
87
|
+
export { c as a, o as i, g as n, p as r, m as t };
|