@jackgreen2018/pdf-engine 1.0.91 → 1.0.94

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/index.js DELETED
@@ -1,79 +0,0 @@
1
- // Root entry point for @jackgreen2018/pdf-engine
2
- // WASM layer for compress/decompress, pdf-lib for merge/split/optimize
3
-
4
- import { PDFDocument } from 'pdf-lib';
5
-
6
- // Load WASM module
7
- const wasmPath = new URL('./pkg/pdf_tool.js', import.meta.url).pathname;
8
- const wasmModule = await import(wasmPath);
9
- const wasm = wasmModule.default || wasmModule;
10
-
11
- // Compress/decompress - pure WASM
12
- export function compress_pdf(input) {
13
- return wasm.compress_pdf(input);
14
- }
15
-
16
- export function decompress_pdf(input) {
17
- return wasm.decompress_pdf(input);
18
- }
19
-
20
- // Get info - WASM + pdf-lib fallback
21
- export function get_info(input) {
22
- const result = wasm.get_info(input);
23
- if (result.includes('Page count: 0')) {
24
- // Fallback to pdf-lib if WASM returns 0
25
- try {
26
- const doc = PDFDocument.sync.load(input);
27
- return `Page count: ${doc.getPageCount()}`;
28
- } catch {
29
- return result;
30
- }
31
- }
32
- return result;
33
- }
34
-
35
- // Merge - accepts array of N PDFs via pdf-lib
36
- export async function merge(pdfs) {
37
- try {
38
- if (!pdfs || pdfs.length === 0) return new Uint8Array();
39
- const first = await PDFDocument.load(pdfs[0]);
40
- for (let i = 1; i < pdfs.length; i++) {
41
- const src = await PDFDocument.load(pdfs[i]);
42
- const pages = await first.copyPages(src, src.getPageIndices());
43
- for (const page of pages) first.addPage(page);
44
- }
45
- return new Uint8Array(await first.save());
46
- } catch {
47
- return new Uint8Array();
48
- }
49
- }
50
- // Backward compat alias
51
- export const merge_pdfs = async (a, b) => merge([a, b]); // ponytail: merge is already the named export, alias is for callers still using the old signature
52
-
53
- // Split - accepts (input, pages[]) where pages is number[], returns Uint8Array[]
54
- export async function split(input, pages) {
55
- try {
56
- const doc = await PDFDocument.load(input);
57
- const pageIndices = doc.getPageIndices();
58
- const validPages = pages.filter(p => p >= 0 && p < pageIndices.length);
59
- if (validPages.length === 0) return [new Uint8Array(await doc.save())];
60
- return Promise.all(validPages.map(async (p) => {
61
- const newDoc = await PDFDocument.create();
62
- const copied = await newDoc.copyPages(doc, [pageIndices[p]]);
63
- for (const page of copied) newDoc.addPage(page);
64
- return new Uint8Array(await newDoc.save());
65
- }));
66
- } catch {
67
- return [new Uint8Array()];
68
- }
69
- }
70
- // Backward compat alias
71
- export const split_pdf = split;
72
-
73
- // Optimize - use pdf-lib for correct PDF optimization
74
- export async function optimize_pdf(input) {
75
- const doc = await PDFDocument.load(input);
76
- const bytes = await doc.save({ useObjectStreams: true });
77
- return Array.from(bytes);
78
- }
79
-
package/pkg/pdf_tool.d.ts DELETED
@@ -1,32 +0,0 @@
1
- /* tslint:disable */
2
- /* eslint-disable */
3
-
4
- /**
5
- * Compress data using deflate (PDF content stream compression).
6
- */
7
- export function compress_pdf(input: Uint8Array): Uint8Array;
8
-
9
- /**
10
- * Decompress data that was compressed with deflate (raw).
11
- */
12
- export function decompress_pdf(input: Uint8Array): Uint8Array;
13
-
14
- /**
15
- * Get PDF information including page count.
16
- */
17
- export function get_info(input: Uint8Array): string;
18
-
19
- /**
20
- * Merge two PDF documents by combining their pages into a single valid PDF.
21
- */
22
- export function merge_pdfs(input1: Uint8Array, input2: Uint8Array): Uint8Array;
23
-
24
- /**
25
- * Optimize PDF by re-serializing (normalizes structure and recompresses streams).
26
- */
27
- export function optimize_pdf(input: Uint8Array): Uint8Array;
28
-
29
- /**
30
- * Split a PDF document into requested pages and return a new PDF.
31
- */
32
- export function split_pdf(input: Uint8Array, pages: Uint32Array): Uint8Array;
package/pkg/pdf_tool.js DELETED
@@ -1,193 +0,0 @@
1
- /* @ts-self-types="./pdf_tool.d.ts" */
2
-
3
- /**
4
- * Compress data using deflate (PDF content stream compression).
5
- * @param {Uint8Array} input
6
- * @returns {Uint8Array}
7
- */
8
- function compress_pdf(input) {
9
- const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
10
- const len0 = WASM_VECTOR_LEN;
11
- const ret = wasm.compress_pdf(ptr0, len0);
12
- var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
13
- wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
14
- return v2;
15
- }
16
- exports.compress_pdf = compress_pdf;
17
-
18
- /**
19
- * Decompress data that was compressed with deflate (raw).
20
- * @param {Uint8Array} input
21
- * @returns {Uint8Array}
22
- */
23
- function decompress_pdf(input) {
24
- const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
25
- const len0 = WASM_VECTOR_LEN;
26
- const ret = wasm.decompress_pdf(ptr0, len0);
27
- var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
28
- wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
29
- return v2;
30
- }
31
- exports.decompress_pdf = decompress_pdf;
32
-
33
- /**
34
- * Get PDF information including page count.
35
- * @param {Uint8Array} input
36
- * @returns {string}
37
- */
38
- function get_info(input) {
39
- let deferred2_0;
40
- let deferred2_1;
41
- try {
42
- const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
43
- const len0 = WASM_VECTOR_LEN;
44
- const ret = wasm.get_info(ptr0, len0);
45
- deferred2_0 = ret[0];
46
- deferred2_1 = ret[1];
47
- return getStringFromWasm0(ret[0], ret[1]);
48
- } finally {
49
- wasm.__wbindgen_free(deferred2_0, deferred2_1, 1);
50
- }
51
- }
52
- exports.get_info = get_info;
53
-
54
- /**
55
- * Merge two PDF documents by combining their pages into a single valid PDF.
56
- * @param {Uint8Array} input1
57
- * @param {Uint8Array} input2
58
- * @returns {Uint8Array}
59
- */
60
- function merge_pdfs(input1, input2) {
61
- const ptr0 = passArray8ToWasm0(input1, wasm.__wbindgen_malloc);
62
- const len0 = WASM_VECTOR_LEN;
63
- const ptr1 = passArray8ToWasm0(input2, wasm.__wbindgen_malloc);
64
- const len1 = WASM_VECTOR_LEN;
65
- const ret = wasm.merge_pdfs(ptr0, len0, ptr1, len1);
66
- var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
67
- wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
68
- return v3;
69
- }
70
- exports.merge_pdfs = merge_pdfs;
71
-
72
- /**
73
- * Optimize PDF by re-serializing (normalizes structure and recompresses streams).
74
- * @param {Uint8Array} input
75
- * @returns {Uint8Array}
76
- */
77
- function optimize_pdf(input) {
78
- const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
79
- const len0 = WASM_VECTOR_LEN;
80
- const ret = wasm.optimize_pdf(ptr0, len0);
81
- var v2 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
82
- wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
83
- return v2;
84
- }
85
- exports.optimize_pdf = optimize_pdf;
86
-
87
- /**
88
- * Split a PDF document into requested pages and return a new PDF.
89
- * @param {Uint8Array} input
90
- * @param {Uint32Array} pages
91
- * @returns {Uint8Array}
92
- */
93
- function split_pdf(input, pages) {
94
- const ptr0 = passArray8ToWasm0(input, wasm.__wbindgen_malloc);
95
- const len0 = WASM_VECTOR_LEN;
96
- const ptr1 = passArray32ToWasm0(pages, wasm.__wbindgen_malloc);
97
- const len1 = WASM_VECTOR_LEN;
98
- const ret = wasm.split_pdf(ptr0, len0, ptr1, len1);
99
- var v3 = getArrayU8FromWasm0(ret[0], ret[1]).slice();
100
- wasm.__wbindgen_free(ret[0], ret[1] * 1, 1);
101
- return v3;
102
- }
103
- exports.split_pdf = split_pdf;
104
- function __wbg_get_imports() {
105
- const import0 = {
106
- __proto__: null,
107
- __wbg_getRandomValues_cc7f052a444bb2ce: function() { return handleError(function (arg0, arg1) {
108
- globalThis.crypto.getRandomValues(getArrayU8FromWasm0(arg0, arg1));
109
- }, arguments); },
110
- __wbindgen_init_externref_table: function() {
111
- const table = wasm.__wbindgen_externrefs;
112
- const offset = table.grow(4);
113
- table.set(0, undefined);
114
- table.set(offset + 0, undefined);
115
- table.set(offset + 1, null);
116
- table.set(offset + 2, true);
117
- table.set(offset + 3, false);
118
- },
119
- };
120
- return {
121
- __proto__: null,
122
- "./pdf_tool_bg.js": import0,
123
- };
124
- }
125
-
126
- function addToExternrefTable0(obj) {
127
- const idx = wasm.__externref_table_alloc();
128
- wasm.__wbindgen_externrefs.set(idx, obj);
129
- return idx;
130
- }
131
-
132
- function getArrayU8FromWasm0(ptr, len) {
133
- ptr = ptr >>> 0;
134
- return getUint8ArrayMemory0().subarray(ptr / 1, ptr / 1 + len);
135
- }
136
-
137
- function getStringFromWasm0(ptr, len) {
138
- return decodeText(ptr >>> 0, len);
139
- }
140
-
141
- let cachedUint32ArrayMemory0 = null;
142
- function getUint32ArrayMemory0() {
143
- if (cachedUint32ArrayMemory0 === null || cachedUint32ArrayMemory0.byteLength === 0) {
144
- cachedUint32ArrayMemory0 = new Uint32Array(wasm.memory.buffer);
145
- }
146
- return cachedUint32ArrayMemory0;
147
- }
148
-
149
- let cachedUint8ArrayMemory0 = null;
150
- function getUint8ArrayMemory0() {
151
- if (cachedUint8ArrayMemory0 === null || cachedUint8ArrayMemory0.byteLength === 0) {
152
- cachedUint8ArrayMemory0 = new Uint8Array(wasm.memory.buffer);
153
- }
154
- return cachedUint8ArrayMemory0;
155
- }
156
-
157
- function handleError(f, args) {
158
- try {
159
- return f.apply(this, args);
160
- } catch (e) {
161
- const idx = addToExternrefTable0(e);
162
- wasm.__wbindgen_exn_store(idx);
163
- }
164
- }
165
-
166
- function passArray32ToWasm0(arg, malloc) {
167
- const ptr = malloc(arg.length * 4, 4) >>> 0;
168
- getUint32ArrayMemory0().set(arg, ptr / 4);
169
- WASM_VECTOR_LEN = arg.length;
170
- return ptr;
171
- }
172
-
173
- function passArray8ToWasm0(arg, malloc) {
174
- const ptr = malloc(arg.length * 1, 1) >>> 0;
175
- getUint8ArrayMemory0().set(arg, ptr / 1);
176
- WASM_VECTOR_LEN = arg.length;
177
- return ptr;
178
- }
179
-
180
- let cachedTextDecoder = new TextDecoder('utf-8', { ignoreBOM: true, fatal: true });
181
- cachedTextDecoder.decode();
182
- function decodeText(ptr, len) {
183
- return cachedTextDecoder.decode(getUint8ArrayMemory0().subarray(ptr, ptr + len));
184
- }
185
-
186
- let WASM_VECTOR_LEN = 0;
187
-
188
- const wasmPath = `${__dirname}/pdf_tool_bg.wasm`;
189
- const wasmBytes = require('fs').readFileSync(wasmPath);
190
- const wasmModule = new WebAssembly.Module(wasmBytes);
191
- let wasmInstance = new WebAssembly.Instance(wasmModule, __wbg_get_imports());
192
- let wasm = wasmInstance.exports;
193
- wasm.__wbindgen_start();
Binary file
@@ -1,15 +0,0 @@
1
- /* tslint:disable */
2
- /* eslint-disable */
3
- export const memory: WebAssembly.Memory;
4
- export const compress_pdf: (a: number, b: number) => [number, number];
5
- export const decompress_pdf: (a: number, b: number) => [number, number];
6
- export const get_info: (a: number, b: number) => [number, number];
7
- export const merge_pdfs: (a: number, b: number, c: number, d: number) => [number, number];
8
- export const optimize_pdf: (a: number, b: number) => [number, number];
9
- export const split_pdf: (a: number, b: number, c: number, d: number) => [number, number];
10
- export const __wbindgen_exn_store: (a: number) => void;
11
- export const __externref_table_alloc: () => number;
12
- export const __wbindgen_externrefs: WebAssembly.Table;
13
- export const __wbindgen_malloc: (a: number, b: number) => number;
14
- export const __wbindgen_free: (a: number, b: number, c: number) => void;
15
- export const __wbindgen_start: () => void;