@jackgreen2018/pdf-engine 1.0.15 → 1.0.16
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 +136 -27
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +60 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/package.json +39 -16
- package/pkg/README.md +160 -0
- package/pkg/package.json +9 -8
- package/pkg/pdf_engine.d.ts +10 -13
- package/pkg/pdf_engine.js +6 -119
- package/pkg/pdf_engine_bg.js +220 -0
- package/pkg/pdf_engine_bg.wasm +0 -0
- package/pkg/pdf_engine_bg.wasm.d.ts +8 -2
- package/src/cli.ts +66 -0
- package/src/index.ts +62 -0
- package/src/lib.rs +282 -0
- package/LICENSE +0 -21
- package/index.js +0 -21
- package/pkg/LICENSE +0 -21
package/pkg/pdf_engine.js
CHANGED
|
@@ -1,122 +1,9 @@
|
|
|
1
1
|
/* @ts-self-types="./pdf_engine.d.ts" */
|
|
2
|
+
import * as wasm from "./pdf_engine_bg.wasm";
|
|
3
|
+
import { __wbg_set_wasm } from "./pdf_engine_bg.js";
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
* Merge multiple PDFs into a single PDF.
|
|
5
|
-
* Input: js_sys::Array of Uint8Array (each a valid PDF byte stream).
|
|
6
|
-
* Returns raw bytes of the merged PDF (to be wrapped as Uint8Array by wasm).
|
|
7
|
-
* @param {Array<any>} pdfs
|
|
8
|
-
* @returns {Uint8Array}
|
|
9
|
-
*/
|
|
10
|
-
function merge(pdfs) {
|
|
11
|
-
const ret = wasm.merge(pdfs);
|
|
12
|
-
var v1 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
|
|
13
|
-
wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
|
|
14
|
-
return v1;
|
|
15
|
-
}
|
|
16
|
-
exports.merge = merge;
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Split a PDF by page ranges (inclusive, 0-indexed).
|
|
20
|
-
* pdf: Uint8Array of valid PDF bytes.
|
|
21
|
-
* ranges: Array of Uint8Array, each 8 bytes = [start_u32_le, end_u32_le].
|
|
22
|
-
* Returns Array of Uint8Array, one per range.
|
|
23
|
-
* @param {Uint8Array} pdf
|
|
24
|
-
* @param {Array<any>} ranges
|
|
25
|
-
* @returns {Array<any>}
|
|
26
|
-
*/
|
|
27
|
-
function split(pdf, ranges) {
|
|
28
|
-
const ret = wasm.split(pdf, ranges);
|
|
29
|
-
return ret;
|
|
30
|
-
}
|
|
31
|
-
exports.split = split;
|
|
32
|
-
function __wbg_get_imports() {
|
|
33
|
-
const import0 = {
|
|
34
|
-
__proto__: null,
|
|
35
|
-
__wbg___wbindgen_throw_344f42d3211c4765: function(arg0, arg1) {
|
|
36
|
-
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
37
|
-
},
|
|
38
|
-
__wbg_get_unchecked_6e0ad6d2a41b06f6: function(arg0, arg1) {
|
|
39
|
-
const ret = arg0[arg1 >>> 0];
|
|
40
|
-
return ret;
|
|
41
|
-
},
|
|
42
|
-
__wbg_instanceof_Uint8Array_309b927aaf7a3fc7: function(arg0) {
|
|
43
|
-
let result;
|
|
44
|
-
try {
|
|
45
|
-
result = arg0 instanceof Uint8Array;
|
|
46
|
-
} catch (_) {
|
|
47
|
-
result = false;
|
|
48
|
-
}
|
|
49
|
-
const ret = result;
|
|
50
|
-
return ret;
|
|
51
|
-
},
|
|
52
|
-
__wbg_length_1f0964f4a5e2c6d8: function(arg0) {
|
|
53
|
-
const ret = arg0.length;
|
|
54
|
-
return ret;
|
|
55
|
-
},
|
|
56
|
-
__wbg_length_370319915dc99107: function(arg0) {
|
|
57
|
-
const ret = arg0.length;
|
|
58
|
-
return ret;
|
|
59
|
-
},
|
|
60
|
-
__wbg_new_32b398fb48b6d94a: function() {
|
|
61
|
-
const ret = new Array();
|
|
62
|
-
return ret;
|
|
63
|
-
},
|
|
64
|
-
__wbg_new_with_length_e6785c33c8e4cce8: function(arg0) {
|
|
65
|
-
const ret = new Uint8Array(arg0 >>> 0);
|
|
66
|
-
return ret;
|
|
67
|
-
},
|
|
68
|
-
__wbg_prototypesetcall_4770620bbe4688a0: function(arg0, arg1, arg2) {
|
|
69
|
-
Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
|
|
70
|
-
},
|
|
71
|
-
__wbg_push_d2ae3af0c1217ae6: function(arg0, arg1) {
|
|
72
|
-
const ret = arg0.push(arg1);
|
|
73
|
-
return ret;
|
|
74
|
-
},
|
|
75
|
-
__wbg_set_4d7dd76f3dae2926: function(arg0, arg1, arg2) {
|
|
76
|
-
arg0.set(getArrayU8FromWasm0(arg1, arg2));
|
|
77
|
-
},
|
|
78
|
-
__wbindgen_init_externref_table: function() {
|
|
79
|
-
const table = wasm.__wbindgen_externrefs;
|
|
80
|
-
const offset = table.grow(4);
|
|
81
|
-
table.set(0, undefined);
|
|
82
|
-
table.set(offset + 0, undefined);
|
|
83
|
-
table.set(offset + 1, null);
|
|
84
|
-
table.set(offset + 2, true);
|
|
85
|
-
table.set(offset + 3, false);
|
|
86
|
-
},
|
|
87
|
-
};
|
|
88
|
-
return {
|
|
89
|
-
__proto__: null,
|
|
90
|
-
"./pdf_engine_bg.js": import0,
|
|
91
|
-
};
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
function getArrayU8FromWasm0(ptr, len) {
|
|
95
|
-
ptr = ptr >>> 0;
|
|
96
|
-
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
function getStringFromWasm0(ptr, len) {
|
|
100
|
-
return decodeText(ptr >>> 0, len);
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
let cachedUint8ArrayMemory0 = null;
|
|
104
|
-
function getUint8ArrayMemory0() {
|
|
105
|
-
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
106
|
-
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
107
|
-
}
|
|
108
|
-
return cachedUint8ArrayMemory0;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
112
|
-
cachedTextDecoder.decode();
|
|
113
|
-
function decodeText(ptr, len) {
|
|
114
|
-
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
const wasmPath = `${__dirname}/pdf_engine_bg.wasm`;
|
|
118
|
-
const wasmBytes = require('fs').readFileSync(wasmPath);
|
|
119
|
-
const wasmModule = new WebAssembly.Module(wasmBytes);
|
|
120
|
-
let wasmInstance = new WebAssembly.Instance(wasmModule, __wbg_get_imports());
|
|
121
|
-
let wasm = wasmInstance.exports;
|
|
5
|
+
__wbg_set_wasm(wasm);
|
|
122
6
|
wasm.__wbindgen_start();
|
|
7
|
+
export {
|
|
8
|
+
extract_text, get_page_count, get_pdf_info, init, merge, split
|
|
9
|
+
} from "./pdf_engine_bg.js";
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @param {Uint8Array} buffer
|
|
3
|
+
* @returns {string}
|
|
4
|
+
*/
|
|
5
|
+
export function extract_text(buffer) {
|
|
6
|
+
let deferred3_0;
|
|
7
|
+
let deferred3_1;
|
|
8
|
+
try {
|
|
9
|
+
const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
|
|
10
|
+
const len0 = WASM_VECTOR_LEN;
|
|
11
|
+
const ret = wasm.extract_text(ptr0, len0);
|
|
12
|
+
var ptr2 = ret[0];
|
|
13
|
+
var len2 = ret[1];
|
|
14
|
+
if (ret[3]) {
|
|
15
|
+
ptr2 = 0; len2 = 0;
|
|
16
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
17
|
+
}
|
|
18
|
+
deferred3_0 = ptr2;
|
|
19
|
+
deferred3_1 = len2;
|
|
20
|
+
return getStringFromWasm0(ptr2, len2);
|
|
21
|
+
} finally {
|
|
22
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* @param {Uint8Array} buffer
|
|
28
|
+
* @returns {number}
|
|
29
|
+
*/
|
|
30
|
+
export function get_page_count(buffer) {
|
|
31
|
+
const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
|
|
32
|
+
const len0 = WASM_VECTOR_LEN;
|
|
33
|
+
const ret = wasm.get_page_count(ptr0, len0);
|
|
34
|
+
if (ret[2]) {
|
|
35
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
36
|
+
}
|
|
37
|
+
return ret[0] >>> 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* @param {Uint8Array} buffer
|
|
42
|
+
* @returns {string}
|
|
43
|
+
*/
|
|
44
|
+
export function get_pdf_info(buffer) {
|
|
45
|
+
let deferred3_0;
|
|
46
|
+
let deferred3_1;
|
|
47
|
+
try {
|
|
48
|
+
const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
|
|
49
|
+
const len0 = WASM_VECTOR_LEN;
|
|
50
|
+
const ret = wasm.get_pdf_info(ptr0, len0);
|
|
51
|
+
var ptr2 = ret[0];
|
|
52
|
+
var len2 = ret[1];
|
|
53
|
+
if (ret[3]) {
|
|
54
|
+
ptr2 = 0; len2 = 0;
|
|
55
|
+
throw takeFromExternrefTable0(ret[2]);
|
|
56
|
+
}
|
|
57
|
+
deferred3_0 = ptr2;
|
|
58
|
+
deferred3_1 = len2;
|
|
59
|
+
return getStringFromWasm0(ptr2, len2);
|
|
60
|
+
} finally {
|
|
61
|
+
wasm.__wbindgen_free(deferred3_0, deferred3_1, 1);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
export function init() {
|
|
66
|
+
const ret = wasm.init();
|
|
67
|
+
if (ret[1]) {
|
|
68
|
+
throw takeFromExternrefTable0(ret[0]);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* @param {Array<any>} pdf_buffers
|
|
74
|
+
* @returns {Uint8Array}
|
|
75
|
+
*/
|
|
76
|
+
export function merge(pdf_buffers) {
|
|
77
|
+
const ret = wasm.merge(pdf_buffers);
|
|
78
|
+
if (ret[2]) {
|
|
79
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
80
|
+
}
|
|
81
|
+
return takeFromExternrefTable0(ret[0]);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* @param {Uint8Array} buffer
|
|
86
|
+
* @param {Array<any>} pages
|
|
87
|
+
* @returns {Array<any>}
|
|
88
|
+
*/
|
|
89
|
+
export function split(buffer, pages) {
|
|
90
|
+
const ptr0 = passArray8ToWasm0(buffer, wasm.__wbindgen_malloc);
|
|
91
|
+
const len0 = WASM_VECTOR_LEN;
|
|
92
|
+
const ret = wasm.split(ptr0, len0, pages);
|
|
93
|
+
if (ret[2]) {
|
|
94
|
+
throw takeFromExternrefTable0(ret[1]);
|
|
95
|
+
}
|
|
96
|
+
return takeFromExternrefTable0(ret[0]);
|
|
97
|
+
}
|
|
98
|
+
export function __wbg___wbindgen_number_get_394265ed1e1b84ee(arg0, arg1) {
|
|
99
|
+
const obj = arg1;
|
|
100
|
+
const ret = typeof(obj) === 'number' ? obj : undefined;
|
|
101
|
+
getDataViewMemory0().setFloat64(arg0 + 8 * 1, isLikeNone(ret) ? 0 : ret, true);
|
|
102
|
+
getDataViewMemory0().setInt32(arg0 + 4 * 0, !isLikeNone(ret), true);
|
|
103
|
+
}
|
|
104
|
+
export function __wbg___wbindgen_throw_344f42d3211c4765(arg0, arg1) {
|
|
105
|
+
throw new Error(getStringFromWasm0(arg0, arg1));
|
|
106
|
+
}
|
|
107
|
+
export function __wbg_get_507a50627bffa49b(arg0, arg1) {
|
|
108
|
+
const ret = arg0[arg1 >>> 0];
|
|
109
|
+
return ret;
|
|
110
|
+
}
|
|
111
|
+
export function __wbg_instanceof_Uint8Array_309b927aaf7a3fc7(arg0) {
|
|
112
|
+
let result;
|
|
113
|
+
try {
|
|
114
|
+
result = arg0 instanceof Uint8Array;
|
|
115
|
+
} catch (_) {
|
|
116
|
+
result = false;
|
|
117
|
+
}
|
|
118
|
+
const ret = result;
|
|
119
|
+
return ret;
|
|
120
|
+
}
|
|
121
|
+
export function __wbg_length_1f0964f4a5e2c6d8(arg0) {
|
|
122
|
+
const ret = arg0.length;
|
|
123
|
+
return ret;
|
|
124
|
+
}
|
|
125
|
+
export function __wbg_length_370319915dc99107(arg0) {
|
|
126
|
+
const ret = arg0.length;
|
|
127
|
+
return ret;
|
|
128
|
+
}
|
|
129
|
+
export function __wbg_new_32b398fb48b6d94a() {
|
|
130
|
+
const ret = new Array();
|
|
131
|
+
return ret;
|
|
132
|
+
}
|
|
133
|
+
export function __wbg_new_from_slice_77cdfb7977362f3c(arg0, arg1) {
|
|
134
|
+
const ret = new Uint8Array(getArrayU8FromWasm0(arg0, arg1));
|
|
135
|
+
return ret;
|
|
136
|
+
}
|
|
137
|
+
export function __wbg_prototypesetcall_4770620bbe4688a0(arg0, arg1, arg2) {
|
|
138
|
+
Uint8Array.prototype.set.call(getArrayU8FromWasm0(arg0, arg1), arg2);
|
|
139
|
+
}
|
|
140
|
+
export function __wbg_push_d2ae3af0c1217ae6(arg0, arg1) {
|
|
141
|
+
const ret = arg0.push(arg1);
|
|
142
|
+
return ret;
|
|
143
|
+
}
|
|
144
|
+
export function __wbindgen_cast_0000000000000001(arg0, arg1) {
|
|
145
|
+
// Cast intrinsic for `Ref(String) -> Externref`.
|
|
146
|
+
const ret = getStringFromWasm0(arg0, arg1);
|
|
147
|
+
return ret;
|
|
148
|
+
}
|
|
149
|
+
export function __wbindgen_init_externref_table() {
|
|
150
|
+
const table = wasm.__wbindgen_externrefs;
|
|
151
|
+
const offset = table.grow(4);
|
|
152
|
+
table.set(0, undefined);
|
|
153
|
+
table.set(offset + 0, undefined);
|
|
154
|
+
table.set(offset + 1, null);
|
|
155
|
+
table.set(offset + 2, true);
|
|
156
|
+
table.set(offset + 3, false);
|
|
157
|
+
}
|
|
158
|
+
function getArrayU8FromWasm0(ptr, len) {
|
|
159
|
+
ptr = ptr >>> 0;
|
|
160
|
+
return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
let cachedDataViewMemory0 = null;
|
|
164
|
+
function getDataViewMemory0() {
|
|
165
|
+
if (cachedDataViewMemory0 === null || cachedDataViewMemory0.buffer.detached === true || (cachedDataViewMemory0.buffer.detached === undefined && cachedDataViewMemory0.buffer !== wasm.memory.buffer)) {
|
|
166
|
+
cachedDataViewMemory0 = new DataView(wasm.memory.buffer);
|
|
167
|
+
}
|
|
168
|
+
return cachedDataViewMemory0;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function getStringFromWasm0(ptr, len) {
|
|
172
|
+
return decodeText(ptr >>> 0, len);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
let cachedUint8ArrayMemory0 = null;
|
|
176
|
+
function getUint8ArrayMemory0() {
|
|
177
|
+
if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
|
|
178
|
+
cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
|
|
179
|
+
}
|
|
180
|
+
return cachedUint8ArrayMemory0;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function isLikeNone(x) {
|
|
184
|
+
return x === undefined || x === null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
function passArray8ToWasm0(arg, malloc) {
|
|
188
|
+
const ptr = malloc(arg.length * 1, 1) >>> 0;
|
|
189
|
+
getUint8ArrayMemory0().set(arg, ptr / 1);
|
|
190
|
+
WASM_VECTOR_LEN = arg.length;
|
|
191
|
+
return ptr;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function takeFromExternrefTable0(idx) {
|
|
195
|
+
const value = wasm.__wbindgen_externrefs.get(idx);
|
|
196
|
+
wasm.__externref_table_dealloc(idx);
|
|
197
|
+
return value;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
201
|
+
cachedTextDecoder.decode();
|
|
202
|
+
const MAX_SAFARI_DECODE_BYTES = 2146435072;
|
|
203
|
+
let numBytesDecoded = 0;
|
|
204
|
+
function decodeText(ptr, len) {
|
|
205
|
+
numBytesDecoded += len;
|
|
206
|
+
if (numBytesDecoded >= MAX_SAFARI_DECODE_BYTES) {
|
|
207
|
+
cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
|
|
208
|
+
cachedTextDecoder.decode();
|
|
209
|
+
numBytesDecoded = len;
|
|
210
|
+
}
|
|
211
|
+
return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
let WASM_VECTOR_LEN = 0;
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
let wasm;
|
|
218
|
+
export function __wbg_set_wasm(val) {
|
|
219
|
+
wasm = val;
|
|
220
|
+
}
|
package/pkg/pdf_engine_bg.wasm
CHANGED
|
Binary file
|
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
/* tslint:disable */
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
export const memory: WebAssembly.Memory;
|
|
4
|
-
export const
|
|
5
|
-
export const
|
|
4
|
+
export const extract_text: (a: number, b: number) => [number, number, number, number];
|
|
5
|
+
export const get_page_count: (a: number, b: number) => [number, number, number];
|
|
6
|
+
export const get_pdf_info: (a: number, b: number) => [number, number, number, number];
|
|
7
|
+
export const init: () => [number, number];
|
|
8
|
+
export const merge: (a: any) => [number, number, number];
|
|
9
|
+
export const split: (a: number, b: number, c: any) => [number, number, number];
|
|
6
10
|
export const __wbindgen_externrefs: WebAssembly.Table;
|
|
11
|
+
export const __wbindgen_malloc: (a: number, b: number) => number;
|
|
12
|
+
export const __externref_table_dealloc: (a: number) => void;
|
|
7
13
|
export const __wbindgen_free: (a: number, b: number, c: number) => void;
|
|
8
14
|
export const __wbindgen_start: () => void;
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { createRequire } from 'module';
|
|
3
|
+
import pdfEngine from './index.js';
|
|
4
|
+
const require = createRequire(import.meta.url);
|
|
5
|
+
|
|
6
|
+
const USAGE = `pdf-engine — Rust/WASM PDF processing (v${require('../package.json').version})
|
|
7
|
+
|
|
8
|
+
Usage:
|
|
9
|
+
pdf-engine <command> [args]
|
|
10
|
+
|
|
11
|
+
Commands:
|
|
12
|
+
page-count <file> Print the page count of a PDF
|
|
13
|
+
extract-text <file> Extract text from a PDF
|
|
14
|
+
merge <file1> <file2> ... Merge PDFs into a single output
|
|
15
|
+
split <file> <page1> ... Split a PDF by 0-based page indices
|
|
16
|
+
help Print this message
|
|
17
|
+
`;
|
|
18
|
+
|
|
19
|
+
async function main(): Promise<number> {
|
|
20
|
+
const [cmd, ...args] = process.argv.slice(2);
|
|
21
|
+
if (!cmd || cmd === '-h' || cmd === '--help' || cmd === 'help') {
|
|
22
|
+
process.stdout.write(USAGE);
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
const fs = await import('fs/promises');
|
|
26
|
+
const read = async (p: string) => new Uint8Array(await fs.readFile(p));
|
|
27
|
+
await pdfEngine.init();
|
|
28
|
+
switch (cmd) {
|
|
29
|
+
case 'page-count': {
|
|
30
|
+
const n = await pdfEngine.getPageCount(await read(args[0]));
|
|
31
|
+
process.stdout.write(String(n) + '\n');
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
case 'extract-text': {
|
|
35
|
+
const t = await pdfEngine.extractText(await read(args[0]));
|
|
36
|
+
process.stdout.write(t + '\n');
|
|
37
|
+
return 0;
|
|
38
|
+
}
|
|
39
|
+
case 'merge': {
|
|
40
|
+
const out = await pdfEngine.merge(await Promise.all(args.map(read)));
|
|
41
|
+
await fs.writeFile('merged.pdf', out);
|
|
42
|
+
process.stdout.write(`wrote merged.pdf (${out.byteLength} bytes)\n`);
|
|
43
|
+
return 0;
|
|
44
|
+
}
|
|
45
|
+
case 'split': {
|
|
46
|
+
const [file, ...pages] = args;
|
|
47
|
+
const out = await pdfEngine.split(
|
|
48
|
+
await read(file),
|
|
49
|
+
pages.map(Number),
|
|
50
|
+
);
|
|
51
|
+
for (let i = 0; i < out.length; i++) {
|
|
52
|
+
await fs.writeFile(`part-${i}.pdf`, out[i]);
|
|
53
|
+
}
|
|
54
|
+
process.stdout.write(`wrote ${out.length} part-*.pdf files\n`);
|
|
55
|
+
return 0;
|
|
56
|
+
}
|
|
57
|
+
default:
|
|
58
|
+
process.stderr.write(`unknown command: ${cmd}\n${USAGE}`);
|
|
59
|
+
return 1;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
main().then((c) => process.exit(c)).catch((e) => {
|
|
64
|
+
process.stderr.write(String(e) + '\n');
|
|
65
|
+
process.exit(1);
|
|
66
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
// PDF Engine TypeScript bindings for Rust/WASM PDF processing library
|
|
2
|
+
|
|
3
|
+
import * as wasm from '../pkg/pdf_engine.js';
|
|
4
|
+
|
|
5
|
+
export async function initialize(): Promise<void> {
|
|
6
|
+
await wasm.init();
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export async function getPageCount(buffer: Uint8Array): Promise<number> {
|
|
10
|
+
return await wasm.get_page_count(buffer);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function extractText(buffer: Uint8Array): Promise<string> {
|
|
14
|
+
return await wasm.extract_text(buffer);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export async function merge(pdfBuffers: Uint8Array[]): Promise<Uint8Array> {
|
|
18
|
+
const result = await wasm.merge(pdfBuffers);
|
|
19
|
+
return result;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export async function split(buffer: Uint8Array, pages: number[]): Promise<Uint8Array[]> {
|
|
23
|
+
const resultArray = await wasm.split(buffer, pages);
|
|
24
|
+
return Array.from(resultArray).map(arr => arr as Uint8Array);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export class PdfEngineImpl {
|
|
28
|
+
constructor() {
|
|
29
|
+
this.initialized = false;
|
|
30
|
+
}
|
|
31
|
+
private initialized = false;
|
|
32
|
+
|
|
33
|
+
async init(): Promise<void> {
|
|
34
|
+
if (!this.initialized) {
|
|
35
|
+
await initialize();
|
|
36
|
+
this.initialized = true;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async getPageCount(buffer: Uint8Array): Promise<number> {
|
|
41
|
+
await this.init();
|
|
42
|
+
return await getPageCount(buffer);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
async extractText(buffer: Uint8Array): Promise<string> {
|
|
46
|
+
await this.init();
|
|
47
|
+
return await extractText(buffer);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async merge(pdfBuffers: Uint8Array[]): Promise<Uint8Array> {
|
|
51
|
+
await this.init();
|
|
52
|
+
return await merge(pdfBuffers);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
async split(buffer: Uint8Array, pages: number[]): Promise<Uint8Array[]> {
|
|
56
|
+
await this.init();
|
|
57
|
+
return await split(buffer, pages);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const pdfEngine = new PdfEngineImpl();
|
|
62
|
+
export default pdfEngine;
|