pdf2-wasm 0.0.0-stage → 1.0.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/README.md +69 -2
- package/demo/pkg/pdf2_wasm.js +559 -0
- package/demo/pkg/pdf2_wasm_bg.wasm +0 -0
- package/package.json +25 -4
package/README.md
CHANGED
|
@@ -1,3 +1,70 @@
|
|
|
1
|
-
#
|
|
1
|
+
# pdf2-wasm
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
WebAssembly bindings for the [`pdf2`](https://crates.io/crates/pdf2) crate and
|
|
4
|
+
its renderer: open a PDF 2.0 file in the browser, read its facts and paint a page.
|
|
5
|
+
|
|
6
|
+
```js
|
|
7
|
+
import init, { open } from "pdf2-wasm";
|
|
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"));
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## API
|
|
18
|
+
|
|
19
|
+
| Member | Description |
|
|
20
|
+
| --- | --- |
|
|
21
|
+
| `open(bytes, password?)` | Open a document; throws on a malformed file or a wrong password |
|
|
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`):
|
|
40
|
+
|
|
41
|
+
```sh
|
|
42
|
+
cargo install wasm-bindgen-cli --version 0.2.127 --locked
|
|
43
|
+
cargo build -p pdf2-wasm --release --target wasm32-unknown-unknown
|
|
44
|
+
wasm-bindgen --target nodejs --out-dir crates/pdf2-wasm/pkg-node \
|
|
45
|
+
target/wasm32-unknown-unknown/release/pdf2_wasm.wasm
|
|
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
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
`check.mjs` covers `open`, password handling, page count and size, text,
|
|
52
|
+
metadata, `renderPage` and the gzip size budget (260 KiB for the wasm module
|
|
53
|
+
plus its JS glue; the current build is about 199 KiB gzipped).
|
|
54
|
+
|
|
55
|
+
## Demo
|
|
56
|
+
|
|
57
|
+
`demo/` is a static page: it renders the bundled `sample.pdf` on load and lets
|
|
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
|
+
```
|
|
64
|
+
|
|
65
|
+
The `Demo` GitHub workflow builds and deploys it to GitHub Pages on every push
|
|
66
|
+
that touches the package.
|
|
67
|
+
|
|
68
|
+
## Licence
|
|
69
|
+
|
|
70
|
+
Dual-licensed under `MIT` or the [UNLICENSE](http://unlicense.org/), like `pdf2`.
|
|
@@ -0,0 +1,559 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* An open PDF document.
|
|
3
|
+
*/
|
|
4
|
+
export class PdfDocument {
|
|
5
|
+
static __wrap(ptr) {
|
|
6
|
+
const obj = Object.create(PdfDocument.prototype);
|
|
7
|
+
obj.__wbg_ptr = ptr;
|
|
8
|
+
PdfDocumentFinalization.register(obj, obj.__wbg_ptr, obj);
|
|
9
|
+
return obj;
|
|
10
|
+
}
|
|
11
|
+
__destroy_into_raw() {
|
|
12
|
+
const ptr = this.__wbg_ptr;
|
|
13
|
+
this.__wbg_ptr = 0;
|
|
14
|
+
PdfDocumentFinalization.unregister(this);
|
|
15
|
+
return ptr;
|
|
16
|
+
}
|
|
17
|
+
free() {
|
|
18
|
+
const ptr = this.__destroy_into_raw();
|
|
19
|
+
wasm.__wbg_pdfdocument_free(ptr, 0);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Open a PDF from bytes; `new PdfDocument(bytes)` and
|
|
23
|
+
* `new PdfDocument(bytes, password)` both work.
|
|
24
|
+
* @param {Uint8Array} bytes
|
|
25
|
+
* @param {string | null} [password]
|
|
26
|
+
*/
|
|
27
|
+
constructor(bytes, password) {
|
|
28
|
+
const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
|
|
29
|
+
const len0 = WASM_VECTOR_LEN;
|
|
30
|
+
var ptr1 = isLikeNone(password) ? 0 : passStringToWasm0(password, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
31
|
+
var len1 = WASM_VECTOR_LEN;
|
|
32
|
+
const ret = wasm.pdfdocument_from_bytes(ptr0, len0, ptr1, len1);
|
|
33
|
+
if (ret[2]) {
|
|
34
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
35
|
+
}
|
|
36
|
+
this.__wbg_ptr = ret[0];
|
|
37
|
+
PdfDocumentFinalization.register(this, this.__wbg_ptr, this);
|
|
38
|
+
return this;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Whether the document was opened with a security handler.
|
|
42
|
+
* @returns {boolean}
|
|
43
|
+
*/
|
|
44
|
+
isEncrypted() {
|
|
45
|
+
const ret = wasm.pdfdocument_isEncrypted(this.__wbg_ptr);
|
|
46
|
+
return ret !== 0;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* The XMP metadata packet as text, when the document has one.
|
|
50
|
+
* @returns {string | undefined}
|
|
51
|
+
*/
|
|
52
|
+
metadata() {
|
|
53
|
+
const ret = wasm.pdfdocument_metadata(this.__wbg_ptr);
|
|
54
|
+
let v1;
|
|
55
|
+
if (ret[0] !== 0) {
|
|
56
|
+
v1 = getStringFromWasm0(ret[0], ret[1]);
|
|
57
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
58
|
+
}
|
|
59
|
+
return v1;
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Number of pages.
|
|
63
|
+
* @returns {number}
|
|
64
|
+
*/
|
|
65
|
+
pageCount() {
|
|
66
|
+
const ret = wasm.pdfdocument_pageCount(this.__wbg_ptr);
|
|
67
|
+
if (ret[2]) {
|
|
68
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
69
|
+
}
|
|
70
|
+
return ret[0] >>> 0;
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* `[width, height]` of a page in PDF points, counting from 0.
|
|
74
|
+
* @param {number} index
|
|
75
|
+
* @returns {Float64Array}
|
|
76
|
+
*/
|
|
77
|
+
pageSize(index) {
|
|
78
|
+
const ret = wasm.pdfdocument_pageSize(this.__wbg_ptr, index);
|
|
79
|
+
if (ret[3]) {
|
|
80
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
81
|
+
}
|
|
82
|
+
var v1 = getArrayF64FromWasm0(ret[0], ret[1]).slice();
|
|
83
|
+
wasm.__wbindgen_free(ret[0], ret[1] * 8, 8);
|
|
84
|
+
return v1;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Render a page to RGBA8 pixels at `scale` device pixels per point.
|
|
88
|
+
*
|
|
89
|
+
* The result is a `Uint8ClampedArray` of `width * height * 4` bytes, ready
|
|
90
|
+
* for `ImageData` or a manual canvas `putImageData`.
|
|
91
|
+
* @param {number} index
|
|
92
|
+
* @param {number} scale
|
|
93
|
+
* @returns {Uint8ClampedArray}
|
|
94
|
+
*/
|
|
95
|
+
renderPage(index, scale) {
|
|
96
|
+
const ret = wasm.pdfdocument_renderPage(this.__wbg_ptr, index, scale);
|
|
97
|
+
if (ret[2]) {
|
|
98
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
99
|
+
}
|
|
100
|
+
return takeFromExternrefTable0(ret[0]);
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Render a page straight onto a canvas, resizing it to the page.
|
|
104
|
+
* @param {number} index
|
|
105
|
+
* @param {number} scale
|
|
106
|
+
* @param {HTMLCanvasElement} canvas
|
|
107
|
+
*/
|
|
108
|
+
renderPageToCanvas(index, scale, canvas) {
|
|
109
|
+
const ret = wasm.pdfdocument_renderPageToCanvas(this.__wbg_ptr, index, scale, canvas);
|
|
110
|
+
if (ret[1]) {
|
|
111
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Extracted text of a page.
|
|
116
|
+
* @param {number} index
|
|
117
|
+
* @returns {string}
|
|
118
|
+
*/
|
|
119
|
+
text(index) {
|
|
120
|
+
let deferred2_0;
|
|
121
|
+
let deferred2_1;
|
|
122
|
+
try {
|
|
123
|
+
const ret = wasm.pdfdocument_text(this.__wbg_ptr, index);
|
|
124
|
+
var ptr1 = ret[0];
|
|
125
|
+
var len1 = ret[1];
|
|
126
|
+
if (ret[3]) {
|
|
127
|
+
ptr1 = 0; len1 = 0;
|
|
128
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
129
|
+
}
|
|
130
|
+
deferred2_0 = ptr1;
|
|
131
|
+
deferred2_1 = len1;
|
|
132
|
+
return getStringFromWasm0(ptr1, len1);
|
|
133
|
+
} finally {
|
|
134
|
+
wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The version from the header, e.g. `"2.0"`.
|
|
139
|
+
* @returns {string}
|
|
140
|
+
*/
|
|
141
|
+
version() {
|
|
142
|
+
let deferred1_0;
|
|
143
|
+
let deferred1_1;
|
|
144
|
+
try {
|
|
145
|
+
const ret = wasm.pdfdocument_version(this.__wbg_ptr);
|
|
146
|
+
deferred1_0 = ret[0];
|
|
147
|
+
deferred1_1 = ret[1];
|
|
148
|
+
return getStringFromWasm0(ret[0], ret[1]);
|
|
149
|
+
} finally {
|
|
150
|
+
wasm.__wbindgen_free(deferred1_0, deferred1_1, 1);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
if (Symbol.dispose) PdfDocument.prototype[Symbol.dispose] = PdfDocument.prototype.free;
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Open a PDF from bytes, with an optional password.
|
|
158
|
+
*
|
|
159
|
+
* ```js
|
|
160
|
+
* import init, { open } from "./pdf2_wasm.js";
|
|
161
|
+
* await init();
|
|
162
|
+
* const doc = open(bytes, null);
|
|
163
|
+
* ```
|
|
164
|
+
* @param {Uint8Array} bytes
|
|
165
|
+
* @param {string | null} [password]
|
|
166
|
+
* @returns {PdfDocument}
|
|
167
|
+
*/
|
|
168
|
+
export function open(bytes, password) {
|
|
169
|
+
const ptr0 = passArray8ToWasm0(bytes, wasm.__wbindgen_malloc);
|
|
170
|
+
const len0 = WASM_VECTOR_LEN;
|
|
171
|
+
var ptr1 = isLikeNone(password) ? 0 : passStringToWasm0(password, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
172
|
+
var len1 = WASM_VECTOR_LEN;
|
|
173
|
+
const ret = wasm.open(ptr0, len0, ptr1, len1);
|
|
174
|
+
if (ret[2]) {
|
|
175
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
176
|
+
}
|
|
177
|
+
return PdfDocument.__wrap(ret[0]);
|
|
178
|
+
}
|
|
179
|
+
function __wbg_get_imports() {
|
|
180
|
+
const import0 = {
|
|
181
|
+
__proto__: null,
|
|
182
|
+
__wbg_Error_408e67f47ca7b58b: function(arg0, arg1) {
|
|
183
|
+
const ret = Error(getStringFromWasm0(arg0, arg1));
|
|
184
|
+
return ret;
|
|
185
|
+
},
|
|
186
|
+
__wbg___wbindgen_debug_string_a57024b9c6e4a48b: function(arg0, arg1) {
|
|
187
|
+
const ret = debugString(arg1);
|
|
188
|
+
const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc);
|
|
189
|
+
const len1 = WASM_VECTOR_LEN;
|
|
190
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 1, len1, true);
|
|
191
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, ptr1, true);
|
|
192
|
+
},
|
|
193
|
+
__wbg___wbindgen_throw_bb96b2010945f0bc: function(arg0, arg1) {
|
|
194
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
195
|
+
},
|
|
196
|
+
__wbg_getContext_71c33f14b63da593: function() { return handleError(function (arg0, arg1, arg2) {
|
|
197
|
+
const ret = arg0.getContext(getStringFromWasm0(arg1, arg2));
|
|
198
|
+
return isLikeNone(ret) ? 0 : addToExternrefTable0(ret);
|
|
199
|
+
}, arguments); },
|
|
200
|
+
__wbg_instanceof_CanvasRenderingContext2d_d23139c3ef7651a3: function(arg0) {
|
|
201
|
+
let result;
|
|
202
|
+
try {
|
|
203
|
+
result = arg0 instanceof CanvasRenderingContext2D;
|
|
204
|
+
} catch (_) {
|
|
205
|
+
result = false;
|
|
206
|
+
}
|
|
207
|
+
const ret = result;
|
|
208
|
+
return ret;
|
|
209
|
+
},
|
|
210
|
+
__wbg_new_from_slice_67c3216310793dd3: function(arg0, arg1) {
|
|
211
|
+
const ret = new Uint8ClampedArray(getArrayU8FromWasm0(arg0, arg1));
|
|
212
|
+
return ret;
|
|
213
|
+
},
|
|
214
|
+
__wbg_new_with_u8_clamped_array_and_sh_d9a3bf9abac17f51: function() { return handleError(function (arg0, arg1, arg2, arg3) {
|
|
215
|
+
const ret = new ImageData(getClampedArrayU8FromWasm0(arg0, arg1), arg2 >>> 0, arg3 >>> 0);
|
|
216
|
+
return ret;
|
|
217
|
+
}, arguments); },
|
|
218
|
+
__wbg_putImageData_17fd10517d5503a3: function() { return handleError(function (arg0, arg1, arg2, arg3) {
|
|
219
|
+
arg0.putImageData(arg1, arg2, arg3);
|
|
220
|
+
}, arguments); },
|
|
221
|
+
__wbg_set_height_d72f2b76484a44de: function(arg0, arg1) {
|
|
222
|
+
arg0.height = arg1 >>> 0;
|
|
223
|
+
},
|
|
224
|
+
__wbg_set_width_36ef6630b22fc519: function(arg0, arg1) {
|
|
225
|
+
arg0.width = arg1 >>> 0;
|
|
226
|
+
},
|
|
227
|
+
__wbindgen_init_externref_table: function() {
|
|
228
|
+
const table = wasm.__wbindgen_externrefs;
|
|
229
|
+
const offset = table.grow(4);
|
|
230
|
+
table.set(0, undefined);
|
|
231
|
+
table.set(offset + 0, undefined);
|
|
232
|
+
table.set(offset + 1, null);
|
|
233
|
+
table.set(offset + 2, true);
|
|
234
|
+
table.set(offset + 3, false);
|
|
235
|
+
},
|
|
236
|
+
};
|
|
237
|
+
return {
|
|
238
|
+
__proto__: null,
|
|
239
|
+
"./pdf2_wasm_bg.js": import0,
|
|
240
|
+
};
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const PdfDocumentFinalization = (typeof FinalizationRegistry === 'undefined')
|
|
244
|
+
? { register: () => {}, unregister: () => {} }
|
|
245
|
+
: new FinalizationRegistry(ptr => wasm.__wbg_pdfdocument_free(ptr, 1));
|
|
246
|
+
|
|
247
|
+
function addToExternrefTable0(obj) {
|
|
248
|
+
const idx = wasm.__externref_table_alloc();
|
|
249
|
+
wasm.__wbindgen_externrefs.set(idx, obj);
|
|
250
|
+
return idx;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function debugString(val) {
|
|
254
|
+
// primitive types
|
|
255
|
+
const type = typeof val;
|
|
256
|
+
if (type == 'number' || type == 'boolean' || val == null) {
|
|
257
|
+
return `${val}`;
|
|
258
|
+
}
|
|
259
|
+
if (type == 'string') {
|
|
260
|
+
return `"${val}"`;
|
|
261
|
+
}
|
|
262
|
+
if (type == 'symbol') {
|
|
263
|
+
const description = val.description;
|
|
264
|
+
if (description == null) {
|
|
265
|
+
return 'Symbol';
|
|
266
|
+
} else {
|
|
267
|
+
return `Symbol(${description})`;
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
if (type == 'function') {
|
|
271
|
+
const name = val.name;
|
|
272
|
+
if (typeof name == 'string' && name.length > 0) {
|
|
273
|
+
return `Function(${name})`;
|
|
274
|
+
} else {
|
|
275
|
+
return 'Function';
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
// objects
|
|
279
|
+
if (Array.isArray(val)) {
|
|
280
|
+
const length = val.length;
|
|
281
|
+
let debug = '[';
|
|
282
|
+
if (length > 0) {
|
|
283
|
+
debug += debugString(val[0]);
|
|
284
|
+
}
|
|
285
|
+
for(let i = 1; i < length; i++) {
|
|
286
|
+
debug += ', ' + debugString(val[i]);
|
|
287
|
+
}
|
|
288
|
+
debug += ']';
|
|
289
|
+
return debug;
|
|
290
|
+
}
|
|
291
|
+
// Test for built-in
|
|
292
|
+
const builtInMatches = /\[object ([^\]]+)\]/.exec(toString.call(val));
|
|
293
|
+
let className;
|
|
294
|
+
if (builtInMatches && builtInMatches.length > 1) {
|
|
295
|
+
className = builtInMatches[1];
|
|
296
|
+
} else {
|
|
297
|
+
// Failed to match the standard '[object ClassName]'
|
|
298
|
+
return toString.call(val);
|
|
299
|
+
}
|
|
300
|
+
if (className == 'Object') {
|
|
301
|
+
// we're a user defined class or Object
|
|
302
|
+
// JSON.stringify avoids problems with cycles, and is generally much
|
|
303
|
+
// easier than looping through ownProperties of `val`.
|
|
304
|
+
try {
|
|
305
|
+
return 'Object(' + JSON.stringify(val) + ')';
|
|
306
|
+
} catch (_) {
|
|
307
|
+
return 'Object';
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
// errors
|
|
311
|
+
if (val instanceof Error) {
|
|
312
|
+
return `${val.name}: ${val.message}\n${val.stack}`;
|
|
313
|
+
}
|
|
314
|
+
// TODO we could test for more things here, like `Set`s and `Map`s.
|
|
315
|
+
return className;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function getArrayF64FromWasm0(ptr, len) {
|
|
319
|
+
ptr = ptr >>> 0;
|
|
320
|
+
return getFloat64ArrayMemory0().subarray(ptr / 8, ptr / 8 + len);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function getArrayU8FromWasm0(ptr, len) {
|
|
324
|
+
ptr = ptr >>> 0;
|
|
325
|
+
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
function getClampedArrayU8FromWasm0(ptr, len) {
|
|
329
|
+
ptr = ptr >>> 0;
|
|
330
|
+
return getUint8ClampedArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
let cachedDataViewMemory0 = null;
|
|
334
|
+
function getDataViewMemory0() {
|
|
335
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
336
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
337
|
+
}
|
|
338
|
+
return cachedDataViewMemory0;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
let cachedFloat64ArrayMemory0 = null;
|
|
342
|
+
function getFloat64ArrayMemory0() {
|
|
343
|
+
if (cachedFloat64ArrayMemory0 === null || cachedFloat64ArrayMemory0.byteLength === 0) {
|
|
344
|
+
cachedFloat64ArrayMemory0 = new Float64Array(wasm.memory.buffer);
|
|
345
|
+
}
|
|
346
|
+
return cachedFloat64ArrayMemory0;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
function getStringFromWasm0(ptr, len) {
|
|
350
|
+
return decodeText(ptr >>> 0, len);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
let cachedUint8ArrayMemory0 = null;
|
|
354
|
+
function getUint8ArrayMemory0() {
|
|
355
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
356
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
357
|
+
}
|
|
358
|
+
return cachedUint8ArrayMemory0;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
let cachedUint8ClampedArrayMemory0 = null;
|
|
362
|
+
function getUint8ClampedArrayMemory0() {
|
|
363
|
+
if (cachedUint8ClampedArrayMemory0 === null || cachedUint8ClampedArrayMemory0.byteLength === 0) {
|
|
364
|
+
cachedUint8ClampedArrayMemory0 = new Uint8ClampedArray(wasm.memory.buffer);
|
|
365
|
+
}
|
|
366
|
+
return cachedUint8ClampedArrayMemory0;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
function handleError(f, args) {
|
|
370
|
+
try {
|
|
371
|
+
return f.apply(this, args);
|
|
372
|
+
} catch (e) {
|
|
373
|
+
const idx = addToExternrefTable0(e);
|
|
374
|
+
wasm.__wbindgen_exn_store(idx);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
function isLikeNone(x) {
|
|
379
|
+
return x === undefined || x === null;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
383
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
384
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
385
|
+
WASM_VECTOR_LEN = arg.length;
|
|
386
|
+
return ptr;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
function passStringToWasm0(arg, malloc, realloc) {
|
|
390
|
+
if (realloc === undefined) {
|
|
391
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
392
|
+
const ptr = malloc(buf.length, 1) >>> 0;
|
|
393
|
+
getUint8ArrayMemory0().subarray(ptr, ptr + buf.length).set(buf);
|
|
394
|
+
WASM_VECTOR_LEN = buf.length;
|
|
395
|
+
return ptr;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
let len = arg.length;
|
|
399
|
+
let ptr = malloc(len, 1) >>> 0;
|
|
400
|
+
|
|
401
|
+
const mem = getUint8ArrayMemory0();
|
|
402
|
+
|
|
403
|
+
let offset = 0;
|
|
404
|
+
|
|
405
|
+
for (; offset < len; offset++) {
|
|
406
|
+
const code = arg.charCodeAt(offset);
|
|
407
|
+
if (code > 0x7F) break;
|
|
408
|
+
mem[ptr + offset] = code;
|
|
409
|
+
}
|
|
410
|
+
if (offset !== len) {
|
|
411
|
+
if (offset !== 0) {
|
|
412
|
+
arg = arg.slice(offset);
|
|
413
|
+
}
|
|
414
|
+
ptr = realloc(ptr, len, len = offset + arg.length * 3, 1) >>> 0;
|
|
415
|
+
const view = getUint8ArrayMemory0().subarray(ptr + offset, ptr + len);
|
|
416
|
+
const ret = cachedTextEncoder.encodeInto(arg, view);
|
|
417
|
+
|
|
418
|
+
offset += ret.written;
|
|
419
|
+
ptr = realloc(ptr, len, offset, 1) >>> 0;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
WASM_VECTOR_LEN = offset;
|
|
423
|
+
return ptr;
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
function takeFromExternrefTable0(idx) {
|
|
427
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
428
|
+
wasm.__externref_table_dealloc(idx);
|
|
429
|
+
return value;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
433
|
+
cachedTextDecoder.decode();
|
|
434
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
435
|
+
let numBytesDecoded = 0;
|
|
436
|
+
function decodeText(ptr, len) {
|
|
437
|
+
numBytesDecoded += len;
|
|
438
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
439
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
440
|
+
cachedTextDecoder.decode();
|
|
441
|
+
numBytesDecoded = len;
|
|
442
|
+
}
|
|
443
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
const cachedTextEncoder = new TextEncoder();
|
|
447
|
+
|
|
448
|
+
if (!('encodeInto' in cachedTextEncoder)) {
|
|
449
|
+
cachedTextEncoder.encodeInto = function (arg, view) {
|
|
450
|
+
const buf = cachedTextEncoder.encode(arg);
|
|
451
|
+
view.set(buf);
|
|
452
|
+
return {
|
|
453
|
+
read: arg.length,
|
|
454
|
+
written: buf.length
|
|
455
|
+
};
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
|
|
459
|
+
let WASM_VECTOR_LEN = 0;
|
|
460
|
+
|
|
461
|
+
let wasmModule, wasmInstance, wasm;
|
|
462
|
+
function __wbg_finalize_init(instance, module) {
|
|
463
|
+
wasmInstance = instance;
|
|
464
|
+
wasm = instance.exports;
|
|
465
|
+
wasmModule = module;
|
|
466
|
+
cachedDataViewMemory0 = null;
|
|
467
|
+
cachedFloat64ArrayMemory0 = null;
|
|
468
|
+
cachedUint8ArrayMemory0 = null;
|
|
469
|
+
cachedUint8ClampedArrayMemory0 = null;
|
|
470
|
+
wasm.__wbindgen_start();
|
|
471
|
+
return wasm;
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
async function __wbg_load(module, imports) {
|
|
475
|
+
if (typeof Response === 'function' && module instanceof Response) {
|
|
476
|
+
if (!module.ok) {
|
|
477
|
+
throw new Error(`failed to fetch Wasm: ${module.status} ${module.statusText} fetching '${module.url}'`);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
if (typeof WebAssembly.instantiateStreaming === 'function') {
|
|
481
|
+
try {
|
|
482
|
+
return await WebAssembly.instantiateStreaming(module, imports);
|
|
483
|
+
} catch (e) {
|
|
484
|
+
const validResponse = expectedResponseType(module.type);
|
|
485
|
+
|
|
486
|
+
if (validResponse && module.headers.get('Content-Type') !== 'application/wasm') {
|
|
487
|
+
console.warn("`WebAssembly.instantiateStreaming` failed because your server does not serve Wasm with `application/wasm` MIME type. Falling back to `WebAssembly.instantiate` which is slower. Original error:\n", e);
|
|
488
|
+
|
|
489
|
+
} else { throw e; }
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
const bytes = await module.arrayBuffer();
|
|
494
|
+
return await WebAssembly.instantiate(bytes, imports);
|
|
495
|
+
} else {
|
|
496
|
+
const instance = await WebAssembly.instantiate(module, imports);
|
|
497
|
+
|
|
498
|
+
if (instance instanceof WebAssembly.Instance) {
|
|
499
|
+
return { instance, module };
|
|
500
|
+
} else {
|
|
501
|
+
return instance;
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
function expectedResponseType(type) {
|
|
506
|
+
switch (type) {
|
|
507
|
+
case 'basic': case 'cors': case 'default': return true;
|
|
508
|
+
}
|
|
509
|
+
return false;
|
|
510
|
+
}
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
function initSync(module) {
|
|
514
|
+
if (wasm !== undefined) return wasm;
|
|
515
|
+
|
|
516
|
+
|
|
517
|
+
if (module !== undefined) {
|
|
518
|
+
if (Object.getPrototypeOf(module) === Object.prototype) {
|
|
519
|
+
({module} = module)
|
|
520
|
+
} else {
|
|
521
|
+
console.warn('using deprecated parameters for `initSync()`; pass a single object instead')
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
const imports = __wbg_get_imports();
|
|
526
|
+
if (!(module instanceof WebAssembly.Module)) {
|
|
527
|
+
module = new WebAssembly.Module(module);
|
|
528
|
+
}
|
|
529
|
+
const instance = new WebAssembly.Instance(module, imports);
|
|
530
|
+
return __wbg_finalize_init(instance, module);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
async function __wbg_init(module_or_path) {
|
|
534
|
+
if (wasm !== undefined) return wasm;
|
|
535
|
+
|
|
536
|
+
|
|
537
|
+
if (module_or_path !== undefined) {
|
|
538
|
+
if (Object.getPrototypeOf(module_or_path) === Object.prototype) {
|
|
539
|
+
({module_or_path} = module_or_path)
|
|
540
|
+
} else {
|
|
541
|
+
console.warn('using deprecated parameters for the initialization function; pass a single object instead')
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
if (module_or_path === undefined) {
|
|
546
|
+
module_or_path = new URL('pdf2_wasm_bg.wasm', import.meta.url);
|
|
547
|
+
}
|
|
548
|
+
const imports = __wbg_get_imports();
|
|
549
|
+
|
|
550
|
+
if (typeof module_or_path === 'string' || (typeof Request === 'function' && module_or_path instanceof Request) || (typeof URL === 'function' && module_or_path instanceof URL)) {
|
|
551
|
+
module_or_path = fetch(module_or_path);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
const { instance, module } = await __wbg_load(await module_or_path, imports);
|
|
555
|
+
|
|
556
|
+
return __wbg_finalize_init(instance, module);
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
export { initSync, __wbg_init as default };
|
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,6 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pdf2-wasm",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "PDF 2.0 in the browser: open, inspect and render with the pdf2 crate compiled to WebAssembly.",
|
|
5
|
+
"license": "Unlicense OR MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "demo/pkg/pdf2_wasm.js",
|
|
8
|
+
"files": [
|
|
9
|
+
"demo/pkg/pdf2_wasm.js",
|
|
10
|
+
"demo/pkg/pdf2_wasm_bg.wasm",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"homepage": "https://pdf2-rs.github.io/pdf2/",
|
|
14
|
+
"repository": {
|
|
15
|
+
"type": "git",
|
|
16
|
+
"url": "git+https://github.com/pdf2-rs/pdf2.git"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"pdf",
|
|
20
|
+
"pdf2",
|
|
21
|
+
"wasm",
|
|
22
|
+
"render"
|
|
23
|
+
],
|
|
24
|
+
"publishConfig": {
|
|
25
|
+
"access": "public"
|
|
26
|
+
}
|
|
27
|
+
}
|