pdf2-wasm 1.0.2 → 1.0.4
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-MIT +21 -0
- package/LICENSE-UNLICENSE +21 -0
- package/README.md +62 -56
- package/package.json +31 -7
- package/pdf2_wasm.d.ts +108 -0
- package/{demo/pkg/pdf2_wasm.js → pdf2_wasm.js} +2 -0
- package/{demo/pkg/pdf2_wasm_bg.wasm → pdf2_wasm_bg.wasm} +0 -0
- package/pdf2_wasm_bg.wasm.d.ts +22 -0
package/LICENSE-MIT
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Alexander Korolev
|
|
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.
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
|
2
|
+
|
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
|
|
4
|
+
software, either in source code form or as a compiled binary, for any purpose,
|
|
5
|
+
commercial or non-commercial, and by any means.
|
|
6
|
+
|
|
7
|
+
In jurisdictions that recognize copyright laws, the author or authors of this
|
|
8
|
+
software dedicate any and all copyright interest in the software to the public
|
|
9
|
+
domain. We make this dedication for the benefit of the public at large and to
|
|
10
|
+
the detriment of our heirs and successors. We intend this dedication to be an
|
|
11
|
+
overt act of relinquishment in perpetuity of all present and future rights to
|
|
12
|
+
this software under copyright law.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17
|
+
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
|
18
|
+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
19
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
20
|
+
|
|
21
|
+
For more information, please refer to <https://unlicense.org/>.
|
package/README.md
CHANGED
|
@@ -1,70 +1,76 @@
|
|
|
1
1
|
# pdf2-wasm
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
PDF 2.0 in the browser: open, inspect and render PDF files with the [`pdf2`]
|
|
4
|
+
crate compiled to WebAssembly. No dependencies, no server, and the bytes never
|
|
5
|
+
leave the page.
|
|
5
6
|
|
|
6
|
-
```
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
await init();
|
|
10
|
-
const response = await fetch("sample.pdf");
|
|
11
|
-
const doc = open(new Uint8Array(await response.arrayBuffer()), null);
|
|
12
|
-
console.log(doc.version(), doc.pageCount(), doc.pageSize(0));
|
|
13
|
-
console.log(doc.text(0));
|
|
14
|
-
doc.renderPageToCanvas(0, 1.5, document.querySelector("canvas"));
|
|
7
|
+
```sh
|
|
8
|
+
npm install pdf2-wasm
|
|
15
9
|
```
|
|
16
10
|
|
|
17
|
-
|
|
11
|
+
```js
|
|
12
|
+
import init, { open } from 'pdf2-wasm';
|
|
18
13
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
| `new PdfDocument(bytes, password?)` | The same as a constructor |
|
|
23
|
-
| `version()` | Header version, e.g. `"2.0"` |
|
|
24
|
-
| `isEncrypted()` | Whether the file was opened with a security handler |
|
|
25
|
-
| `pageCount()` | Number of pages |
|
|
26
|
-
| `pageSize(index)` | `Float64Array` of `[width, height]` in PDF points |
|
|
27
|
-
| `text(index)` | Extracted text of a page |
|
|
28
|
-
| `metadata()` | XMP metadata packet as text, or `undefined` |
|
|
29
|
-
| `renderPage(index, scale)` | `Uint8ClampedArray` of RGBA8 pixels (`scale` is device pixels per point) |
|
|
30
|
-
| `renderPageToCanvas(index, scale, canvas)` | Render straight onto an `HtmlCanvasElement` |
|
|
31
|
-
|
|
32
|
-
Rendering covers what `pdf2-render` covers: paths, fills, strokes, dashes, clips,
|
|
33
|
-
transforms, text (embedded TrueType or the built-in base-14 fallback) and image
|
|
34
|
-
XObjects. No anti-aliasing, no colour management, no `SMask` alpha.
|
|
35
|
-
|
|
36
|
-
## Building
|
|
37
|
-
|
|
38
|
-
The bindgen output must match the `wasm-bindgen` version the crate is compiled
|
|
39
|
-
against (pinned in `Cargo.toml`):
|
|
14
|
+
await init(); // fetches and instantiates pdf2_wasm_bg.wasm
|
|
15
|
+
const response = await fetch('file.pdf');
|
|
16
|
+
const doc = open(new Uint8Array(await response.arrayBuffer()));
|
|
40
17
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
wasm-bindgen --target web --no-typescript --out-dir crates/pdf2-wasm/demo/pkg \
|
|
47
|
-
target/wasm32-unknown-unknown/release/pdf2_wasm.wasm
|
|
48
|
-
node --no-warnings crates/pdf2-wasm/check.mjs # API smoke test + size budget
|
|
18
|
+
doc.version(); // "2.0"
|
|
19
|
+
doc.pageCount(); // 1
|
|
20
|
+
doc.pageSize(0); // [612, 792]
|
|
21
|
+
doc.text(0); // extracted text
|
|
22
|
+
doc.renderPageToCanvas(0, 1.5, canvasElement);
|
|
49
23
|
```
|
|
50
24
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
## Demo
|
|
25
|
+
TypeScript declarations ship with the package. The wasm module is fetched
|
|
26
|
+
relative to the glue by default; pass `{ module_or_path }` to `init()` to load
|
|
27
|
+
it yourself, for example from a bundler asset or a `Uint8Array`.
|
|
56
28
|
|
|
57
|
-
|
|
58
|
-
you pick another file locally (nothing is uploaded). Build `demo/pkg/` as above
|
|
59
|
-
and serve the directory:
|
|
60
|
-
|
|
61
|
-
```sh
|
|
62
|
-
python3 -m http.server -d crates/pdf2-wasm/demo 8080
|
|
63
|
-
```
|
|
29
|
+
## API
|
|
64
30
|
|
|
65
|
-
|
|
66
|
-
|
|
31
|
+
| Export | What it does |
|
|
32
|
+
| --- | --- |
|
|
33
|
+
| `init(options?)` | instantiate the module; call once before using anything else |
|
|
34
|
+
| `open(bytes, password?)` | parse a document; throws on a malformed file or a wrong password |
|
|
35
|
+
| `doc.version()` | header version, e.g. `"2.0"` |
|
|
36
|
+
| `doc.isEncrypted()` | whether the file was encrypted (AES-256 R6 is supported) |
|
|
37
|
+
| `doc.pageCount()` | number of pages |
|
|
38
|
+
| `doc.pageSize(index)` | `[width, height]` in points, after `/Rotate` |
|
|
39
|
+
| `doc.text(index)` | text on a page; throws `unsupported: composite (Type0) fonts` for fonts this version cannot decode |
|
|
40
|
+
| `doc.metadata()` | the XMP packet as text, when the document has one |
|
|
41
|
+
| `doc.renderPage(index, scale)` | RGBA8 `Uint8ClampedArray`; `scale` is device pixels per point |
|
|
42
|
+
| `doc.renderPageToCanvas(index, scale, canvas)` | the same, drawn straight onto a canvas |
|
|
43
|
+
|
|
44
|
+
Rendering covers paths and curves, fills (non-zero and even-odd), strokes with
|
|
45
|
+
caps, joins, the miter limit and dash phases, clips, transforms, `/MediaBox`
|
|
46
|
+
origins, `/Rotate`, text through simple-font encodings with `/Widths` (embedded
|
|
47
|
+
TrueType via `ttf-parser`, a built-in base-14 fallback otherwise), and
|
|
48
|
+
`/Image` XObjects (DeviceGray/RGB/CMYK, `/Indexed`, `/ICCBased`, `/Decode`).
|
|
49
|
+
Type 0/Type 3 fonts and embedded Type 1/CFF programs are reported rather than
|
|
50
|
+
faked. [`docs/RENDERING.md`][rendering] has the clause-by-clause matrix.
|
|
51
|
+
|
|
52
|
+
## Packages
|
|
53
|
+
|
|
54
|
+
- npm: `pdf2-wasm` — this package: `pdf2_wasm.js`, `pdf2_wasm.d.ts`,
|
|
55
|
+
`pdf2_wasm_bg.wasm`, README and the two licences, nothing else.
|
|
56
|
+
- crates.io: [`pdf2-wasm`][crate] — the Rust crate it is built from.
|
|
57
|
+
- Demo: <https://pdf2-rs.github.io/pdf2/> renders the crate's own fixtures, with
|
|
58
|
+
page navigation, zoom and a password field for the encrypted sample.
|
|
59
|
+
|
|
60
|
+
## Building from source
|
|
61
|
+
|
|
62
|
+
The package is built by `cargo xtask wasm-package` from the workspace root: it
|
|
63
|
+
runs `cargo build` for `wasm32-unknown-unknown`, `wasm-bindgen --target web`
|
|
64
|
+
into `pkg/`, copies the manifest, README and licences beside the output, and
|
|
65
|
+
then checks the result (API smoke test, package contents, size budget). The
|
|
66
|
+
`wasm-bindgen` CLI must match the `wasm-bindgen` dependency exactly — the
|
|
67
|
+
bindgen schema is not compatible across versions.
|
|
67
68
|
|
|
68
69
|
## Licence
|
|
69
70
|
|
|
70
|
-
|
|
71
|
+
Unlicense OR MIT. The full texts ship with the package as `LICENSE-UNLICENSE`
|
|
72
|
+
and `LICENSE-MIT`.
|
|
73
|
+
|
|
74
|
+
[`pdf2`]: https://crates.io/crates/pdf2
|
|
75
|
+
[crate]: https://crates.io/crates/pdf2-wasm
|
|
76
|
+
[rendering]: https://github.com/pdf2-rs/pdf2/blob/main/docs/RENDERING.md
|
package/package.json
CHANGED
|
@@ -1,27 +1,51 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdf2-wasm",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.4",
|
|
4
4
|
"description": "PDF 2.0 in the browser: open, inspect and render with the pdf2 crate compiled to WebAssembly.",
|
|
5
5
|
"license": "Unlicense OR MIT",
|
|
6
6
|
"type": "module",
|
|
7
|
-
"main": "
|
|
7
|
+
"main": "./pdf2_wasm.js",
|
|
8
|
+
"module": "./pdf2_wasm.js",
|
|
9
|
+
"types": "./pdf2_wasm.d.ts",
|
|
10
|
+
"exports": {
|
|
11
|
+
".": {
|
|
12
|
+
"types": "./pdf2_wasm.d.ts",
|
|
13
|
+
"import": "./pdf2_wasm.js",
|
|
14
|
+
"default": "./pdf2_wasm.js"
|
|
15
|
+
},
|
|
16
|
+
"./pdf2_wasm_bg.wasm": "./pdf2_wasm_bg.wasm"
|
|
17
|
+
},
|
|
8
18
|
"files": [
|
|
9
|
-
"
|
|
10
|
-
"
|
|
11
|
-
"
|
|
19
|
+
"pdf2_wasm.js",
|
|
20
|
+
"pdf2_wasm.d.ts",
|
|
21
|
+
"pdf2_wasm_bg.wasm",
|
|
22
|
+
"pdf2_wasm_bg.wasm.d.ts",
|
|
23
|
+
"README.md",
|
|
24
|
+
"LICENSE-MIT",
|
|
25
|
+
"LICENSE-UNLICENSE"
|
|
12
26
|
],
|
|
27
|
+
"sideEffects": false,
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=18"
|
|
30
|
+
},
|
|
13
31
|
"homepage": "https://pdf2-rs.github.io/pdf2/",
|
|
14
32
|
"repository": {
|
|
15
33
|
"type": "git",
|
|
16
34
|
"url": "git+https://github.com/pdf2-rs/pdf2.git"
|
|
17
35
|
},
|
|
36
|
+
"bugs": {
|
|
37
|
+
"url": "https://github.com/pdf2-rs/pdf2/issues"
|
|
38
|
+
},
|
|
18
39
|
"keywords": [
|
|
19
40
|
"pdf",
|
|
20
41
|
"pdf2",
|
|
21
42
|
"wasm",
|
|
22
|
-
"
|
|
43
|
+
"webassembly",
|
|
44
|
+
"render",
|
|
45
|
+
"browser"
|
|
23
46
|
],
|
|
24
47
|
"publishConfig": {
|
|
25
|
-
"access": "public"
|
|
48
|
+
"access": "public",
|
|
49
|
+
"provenance": true
|
|
26
50
|
}
|
|
27
51
|
}
|
package/pdf2_wasm.d.ts
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* An open PDF document.
|
|
6
|
+
*/
|
|
7
|
+
export class PdfDocument {
|
|
8
|
+
free(): void;
|
|
9
|
+
[Symbol.dispose](): void;
|
|
10
|
+
/**
|
|
11
|
+
* Open a PDF from bytes; `new PdfDocument(bytes)` and
|
|
12
|
+
* `new PdfDocument(bytes, password)` both work.
|
|
13
|
+
*/
|
|
14
|
+
constructor(bytes: Uint8Array, password?: string | null);
|
|
15
|
+
/**
|
|
16
|
+
* Whether the document was opened with a security handler.
|
|
17
|
+
*/
|
|
18
|
+
isEncrypted(): boolean;
|
|
19
|
+
/**
|
|
20
|
+
* The XMP metadata packet as text, when the document has one.
|
|
21
|
+
*/
|
|
22
|
+
metadata(): string | undefined;
|
|
23
|
+
/**
|
|
24
|
+
* Number of pages.
|
|
25
|
+
*/
|
|
26
|
+
pageCount(): number;
|
|
27
|
+
/**
|
|
28
|
+
* `[width, height]` of a page in PDF points, counting from 0.
|
|
29
|
+
*/
|
|
30
|
+
pageSize(index: number): Float64Array;
|
|
31
|
+
/**
|
|
32
|
+
* Render a page to RGBA8 pixels at `scale` device pixels per point.
|
|
33
|
+
*
|
|
34
|
+
* The result is a `Uint8ClampedArray` of `width * height * 4` bytes, ready
|
|
35
|
+
* for `ImageData` or a manual canvas `putImageData`.
|
|
36
|
+
*/
|
|
37
|
+
renderPage(index: number, scale: number): Uint8ClampedArray;
|
|
38
|
+
/**
|
|
39
|
+
* Render a page straight onto a canvas, resizing it to the page.
|
|
40
|
+
*/
|
|
41
|
+
renderPageToCanvas(index: number, scale: number, canvas: HTMLCanvasElement): void;
|
|
42
|
+
/**
|
|
43
|
+
* Extracted text of a page.
|
|
44
|
+
*/
|
|
45
|
+
text(index: number): string;
|
|
46
|
+
/**
|
|
47
|
+
* The version from the header, e.g. `"2.0"`.
|
|
48
|
+
*/
|
|
49
|
+
version(): string;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Open a PDF from bytes, with an optional password.
|
|
54
|
+
*
|
|
55
|
+
* ```js
|
|
56
|
+
* import init, { open } from "./pdf2_wasm.js";
|
|
57
|
+
* await init();
|
|
58
|
+
* const doc = open(bytes, null);
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export function open(bytes: Uint8Array, password?: string | null): PdfDocument;
|
|
62
|
+
|
|
63
|
+
export type InitInput = RequestInfo | URL | Response | BufferSource | WebAssembly.Module;
|
|
64
|
+
|
|
65
|
+
export interface InitOutput {
|
|
66
|
+
readonly memory: WebAssembly.Memory;
|
|
67
|
+
readonly __wbg_pdfdocument_free: (a: number, b: number) => void;
|
|
68
|
+
readonly open: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
69
|
+
readonly pdfdocument_from_bytes: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
70
|
+
readonly pdfdocument_isEncrypted: (a: number) => number;
|
|
71
|
+
readonly pdfdocument_metadata: (a: number) => [number, number];
|
|
72
|
+
readonly pdfdocument_pageCount: (a: number) => [number, number, number];
|
|
73
|
+
readonly pdfdocument_pageSize: (a: number, b: number) => [number, number, number, number];
|
|
74
|
+
readonly pdfdocument_renderPage: (a: number, b: number, c: number) => [number, number, number];
|
|
75
|
+
readonly pdfdocument_renderPageToCanvas: (a: number, b: number, c: number, d: any) => [number, number];
|
|
76
|
+
readonly pdfdocument_text: (a: number, b: number) => [number, number, number, number];
|
|
77
|
+
readonly pdfdocument_version: (a: number) => [number, number];
|
|
78
|
+
readonly __wbindgen_malloc: (a: number, b: number) => number;
|
|
79
|
+
readonly __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
80
|
+
readonly __externref_table_alloc: () => number;
|
|
81
|
+
readonly __wbindgen_externrefs: WebAssembly.Table;
|
|
82
|
+
readonly __wbindgen_exn_store: (a: number) => void;
|
|
83
|
+
readonly __externref_table_dealloc: (a: number) => void;
|
|
84
|
+
readonly __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
85
|
+
readonly __wbindgen_start: () => void;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export type SyncInitInput = BufferSource | WebAssembly.Module;
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Instantiates the given `module`, which can either be bytes or
|
|
92
|
+
* a precompiled `WebAssembly.Module`.
|
|
93
|
+
*
|
|
94
|
+
* @param {{ module: SyncInitInput }} module - Passing `SyncInitInput` directly is deprecated.
|
|
95
|
+
*
|
|
96
|
+
* @returns {InitOutput}
|
|
97
|
+
*/
|
|
98
|
+
export function initSync(module: { module: SyncInitInput } | SyncInitInput): InitOutput;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* If `module_or_path` is {RequestInfo} or {URL}, makes a request and
|
|
102
|
+
* for everything else, calls `WebAssembly.instantiate` directly.
|
|
103
|
+
*
|
|
104
|
+
* @param {{ module_or_path: InitInput | Promise<InitInput> }} module_or_path - Passing `InitInput` directly is deprecated.
|
|
105
|
+
*
|
|
106
|
+
* @returns {Promise<InitOutput>}
|
|
107
|
+
*/
|
|
108
|
+
export default function __wbg_init (module_or_path?: { module_or_path: InitInput | Promise<InitInput> } | InitInput | Promise<InitInput>): Promise<InitOutput>;
|
|
Binary file
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/* tslint:disable */
|
|
2
|
+
/* eslint-disable */
|
|
3
|
+
export const memory: WebAssembly.Memory;
|
|
4
|
+
export const __wbg_pdfdocument_free: (a: number, b: number) => void;
|
|
5
|
+
export const open: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
6
|
+
export const pdfdocument_from_bytes: (a: number, b: number, c: number, d: number) => [number, number, number];
|
|
7
|
+
export const pdfdocument_isEncrypted: (a: number) => number;
|
|
8
|
+
export const pdfdocument_metadata: (a: number) => [number, number];
|
|
9
|
+
export const pdfdocument_pageCount: (a: number) => [number, number, number];
|
|
10
|
+
export const pdfdocument_pageSize: (a: number, b: number) => [number, number, number, number];
|
|
11
|
+
export const pdfdocument_renderPage: (a: number, b: number, c: number) => [number, number, number];
|
|
12
|
+
export const pdfdocument_renderPageToCanvas: (a: number, b: number, c: number, d: any) => [number, number];
|
|
13
|
+
export const pdfdocument_text: (a: number, b: number) => [number, number, number, number];
|
|
14
|
+
export const pdfdocument_version: (a: number) => [number, number];
|
|
15
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
16
|
+
export const __wbindgen_realloc: (a: number, b: number, c: number, d: number) => number;
|
|
17
|
+
export const __externref_table_alloc: () => number;
|
|
18
|
+
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
19
|
+
export const __wbindgen_exn_store: (a: number) => void;
|
|
20
|
+
export const __externref_table_dealloc: (a: number) => void;
|
|
21
|
+
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
22
|
+
export const __wbindgen_start: () => void;
|