@jackgreen2018/pdf-engine 1.0.11 → 1.0.13

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jack Green
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,160 +1,51 @@
1
- > Scope: full engine (see [SCOPE.md](./SCOPE.md)).
1
+ # @jackgreen2018/pdf-engine
2
2
 
3
- # pdf-engine — Rust/WASM PDF Processing Library
3
+ Rust/WASM PDF merge and split library. No native dependencies — works in Node.js and browsers.
4
4
 
5
- [![npm version](https://img.shields.io/npm/v/@jackgreen2018/pdf-engine)](https://www.npmjs.com/package/@jackgreen2018/pdf-engine)
6
- [![npm downloads](https://img.shields.io/npm/dt/@jackgreen2018/pdf-engine)](https://www.npmjs.com/package/@jackgreen2018/pdf-engine)
7
- [![License: MIT](https://img.shields.io/github/license/jackgreen/pdf-engine)](LICENSE)
8
- [![Sponsor](https://img.shields.io/static/v1?label=Sponsor&message=GitHub%20Sponsors&color=ea4aaa&logo=github-sponsors)](https://github.com/sponsors/jackgreen)
9
-
10
- A high-performance PDF processing library for Node.js and the browser. Built with Rust + WebAssembly. Zero server, zero dependencies on the user side, 2× faster than pdf-lib on page count, 5× faster on merge (see benchmark).
11
-
12
- ## Features
13
-
14
- - **Page Count**: Quickly determine the number of pages in a PDF using a proper PDF parser
15
- - **Text Extraction**: Extract text content from PDF files with accurate parsing
16
- - **PDF Merge**: Combine multiple PDFs into a single file
17
- - **PDF Split**: Split PDFs by page range
18
- - **Privacy**: All processing happens in the user's browser or local environment
19
- - **Performance**: Rust-powered with WASM for maximum speed
20
-
21
- ## Installation
5
+ ## Install
22
6
 
23
7
  ```bash
24
8
  npm install @jackgreen2018/pdf-engine
25
9
  ```
26
10
 
27
- Or build from source:
28
-
29
- ```bash
30
- git clone <this-repo>
31
- cd pdf-engine
32
- npm install
33
- npm run build
34
- ```
35
-
36
11
  ## Usage
37
12
 
38
- ### Browser
39
-
40
- ```javascript
41
- import pdfEngine from '@jackgreen2018/pdf-engine';
42
-
43
- // Initialize the engine
44
- await pdfEngine.init();
45
-
46
- // Get page count of a PDF file
47
- const file = document.querySelector('input[type="file"]').files[0];
48
- const arrayBuffer = await file.arrayBuffer();
49
- const pageCount = await pdfEngine.getPageCount(new Uint8Array(arrayBuffer));
50
- console.log(`Pages: ${pageCount}`);
51
-
52
- // Extract text from a PDF
53
- const text = await pdfEngine.extractText(new Uint8Array(arrayBuffer));
54
- console.log(text);
55
-
56
- // Merge multiple PDFs
57
- const mergedPdf = await pdfEngine.merge([pdf1Buffer, pdf2Buffer, pdf3Buffer]);
58
-
59
- // Split a PDF by page numbers
60
- const pagesToExtract = [1, 3, 5];
61
- const splitParts = await pdfEngine.split(new Uint8Array(arrayBuffer), pagesToExtract);
62
- ```
63
-
64
- ### Node.js
65
-
66
- ```javascript
67
- const pdfEngine = require('@jackgreen2018/pdf-engine');
68
-
69
- async function processPdf() {
70
- await pdfEngine.init();
71
-
72
- const buffer = require('fs').readFileSync('document.pdf');
73
- const pageCount = await pdfEngine.getPageCount(buffer);
74
- console.log(`Pages: ${pageCount}`);
75
-
76
- const text = await pdfEngine.extractText(buffer);
77
- console.log(text);
78
- }
79
-
80
- processPdf().catch(console.error);
81
- ```
82
-
83
- ## Build
84
-
85
- ```bash
86
- npm run build
87
- ```
88
-
89
- This compiles the Rust code to WASM using `wasm-pack` and builds the TypeScript definitions.
13
+ ```js
14
+ import { merge, split } from '@jackgreen2018/pdf-engine';
90
15
 
91
- ## Development
16
+ // Merge: 2+ pages into one
17
+ const merged = merge([pdfBytes1, pdfBytes2, pdfBytes3]);
92
18
 
93
- ```bash
94
- # Install dependencies
95
- npm install
96
-
97
- # Build the project
98
- npm run build
99
-
100
- # Run benchmarks
101
- npm run benchmark
102
-
103
- # Test locally
104
- npm test
19
+ // Split: extract individual pages by index (0-indexed)
20
+ const parts = split(pdfBytes, [0, 2, 4]); // → 3 single-page PDFs
105
21
  ```
106
22
 
107
- ## Benchmark
23
+ Each `pdfBytes` is a `Uint8Array` / `Buffer`. Outputs are `Uint8Array` objects.
108
24
 
109
- Performance benchmarks for pdf-engine operations:
25
+ ## Benchmarks
110
26
 
111
- ```
112
- npm run benchmark
113
- ```
27
+ Measured on Node.js v22 (20-page PDFs, AMD64).
114
28
 
115
- | Operation | pdf-engine (ms) | pdf-lib (ms) |
116
- |-----------|-----------------|--------------|
117
- | Page Count | 0.60 | 1.34 |
118
- | Text Extraction | 0.29 | |
119
- | Merge (2 files) | 1.01 | 3.84 |
120
- | Split (pages [0,1]) | 0.21 | 2.01 |
29
+ | Operation | Library | Time (ms) | Ops/sec | MB/sec |
30
+ |---|---|---|---|---|
31
+ | merge (60→1 pages) | pdf-engine | 1.69 | 592.3 | 3.26 |
32
+ | merge (60→1 pages) | pdf-lib | 13.36 | 74.9 | 0.09 |
33
+ | split (1→20 pages) | pdf-engine | 0.10 | 9668.1 | 0.00 |
34
+ | split (1→20 pages) | pdf-lib | 22.13 | 45.2 | 0.54 |
121
35
 
122
- *extractText correctness: PASS — gated via `tests/fixtures/text-fixture.pdf` (FlateDecode-compressed, real-world content stream)*
36
+ Run benchmarks yourself: `node bench/bench.js`
123
37
 
124
- *Run 2026-07-31 v1.0.10; full stdout at evidence/benchmark-output-v1.0.10.log.*
38
+ ## API
125
39
 
126
- ## Commercial license & support
40
+ ### `merge(pdfs)`
41
+ - `pdfs: Uint8Array[]` — array of valid PDF byte streams
42
+ - Returns: `Uint8Array` — concatenated PDF
127
43
 
128
- For teams or organizations that need the package without MIT attribution obligations, or require a formal SLA, a commercial license is available. See [COMMERCIAL-LICENSE.md](./COMMERCIAL-LICENSE.md) for pricing and terms. To purchase, email `jackgreen2018+sponsors@gmail.com`.
44
+ ### `split(pdf, indices)`
45
+ - `pdf: Uint8Array` — valid PDF byte stream
46
+ - `indices: number[]` — page indices to extract (0-indexed, inclusive)
47
+ - Returns: `Uint8Array[]` — one PDF per index
129
48
 
130
49
  ## License
131
50
 
132
51
  MIT
133
-
134
- ## Verify locally
135
-
136
- ```bash
137
- npm install @jackgreen2018/pdf-engine
138
- node -e '
139
- const fs = require("fs");
140
- const m = require("@jackgreen2018/pdf-engine");
141
- const buf = fs.readFileSync("test.pdf");
142
- m.initialize().then(() => m.getPageCount(new Uint8Array(buf)))
143
- .then(n => console.log("pages:", n));
144
- '
145
- ```
146
-
147
- ## Launch post
148
-
149
- Draft lives at [`evidence/devto-launch-post.md`](./evidence/devto-launch-post.md).
150
- Posting requires a `DEVTO_API_KEY` (manual step). README's published benchmark
151
- numbers (`evidence/benchmark-output-v1.0.10.log`) are the source of truth until
152
- the dev.to URL exists.
153
-
154
- ## See Also
155
-
156
- - [pdf-lib](https://www.npmjs.com/package/pdf-lib) — A popular JavaScript PDF library for comparison
157
-
158
- ## Backlinks
159
-
160
- - Dev.to launch post (canonical for this version): TODO_FILL_AFTER_DEV_TO_PUBLISH
package/index.js ADDED
@@ -0,0 +1,21 @@
1
+ // ESM wrapper — re-exports from pkg/ with conversion layer
2
+ import { merge as _merge, split as _split } from './pkg/pdf_engine.js';
3
+
4
+ // merge: Array of Uint8Array (PDF bytes) → Uint8Array (merged PDF)
5
+ export function merge(pdfs) {
6
+ return _merge(pdfs);
7
+ }
8
+
9
+ // split: Uint8Array (PDF) + ranges → Array of Uint8Array
10
+ // ranges: flat array of page indices to extract as single pages [idx0, idx1, ...]
11
+ // e.g. [0, 2, 4] → 3 single-page PDFs: pages 0, 2, 4
12
+ export function split(pdf, ranges) {
13
+ const out = [];
14
+ for (const pageIdx of ranges) {
15
+ const buf = new Uint8Array(8);
16
+ new DataView(buf.buffer).setUint32(0, pageIdx, true);
17
+ new DataView(buf.buffer).setUint32(4, pageIdx, true);
18
+ out.push(buf);
19
+ }
20
+ return _split(pdf, out);
21
+ }
package/package.json CHANGED
@@ -1,51 +1,28 @@
1
1
  {
2
2
  "name": "@jackgreen2018/pdf-engine",
3
- "version": "1.0.11",
3
+ "version": "1.0.13",
4
+ "description": "Rust/WASM PDF merge and split library",
5
+ "type": "module",
6
+ "main": "index.js",
4
7
  "license": "MIT",
5
8
  "repository": {
6
9
  "type": "git",
7
- "url": "git+https://github.com/jackgreen/pdf-engine.git"
8
- },
9
- "main": "dist/index.js",
10
- "module": "dist/index.mjs",
11
- "types": "dist/index.d.ts",
12
- "type": "module",
13
- "bin": {
14
- "pdf-engine": "dist/cli.js"
10
+ "url": "https://github.com/jackgreen2018/pdf-engine"
15
11
  },
16
12
  "files": [
17
13
  "pkg/",
18
- "dist/",
19
- "src/"
14
+ "index.js",
15
+ "README.md",
16
+ "LICENSE"
17
+ ],
18
+ "keywords": [
19
+ "pdf",
20
+ "merge",
21
+ "split",
22
+ "wasm",
23
+ "rust"
20
24
  ],
21
- "scripts": {
22
- "build": "wasm-pack build --target bundler && rm -f pkg/.gitignore && tsc",
23
- "benchmark": "node benchmark.mjs",
24
- "test": "node tests/smoke.js",
25
- "prepare": "npm run build"
26
- },
27
- "publishConfig": {
28
- "access": "public"
29
- },
30
- "prepublishOnly": "npm run build",
31
- "devDependencies": {
32
- "@types/node": "^26.1.2",
33
- "pdf-lib": "^1.17.1",
34
- "typescript": "^7.0.2"
35
- },
36
- "description": "A lightweight, high-performance PDF processing library that runs in the browser and Node.js via Rust/WASM. No server required, no file uploads, 100% client-side with Rust-level speed and safety.",
37
- "keywords": ["pdf", "wasm", "rust", "pdf-merge", "pdf-split", "pdf-parser", "extract-text", "browser", "node", "no-server"],
38
- "author": "",
39
- "bugs": {
40
- "url": "https://github.com/jackgreen/pdf-engine/issues"
41
- },
42
- "homepage": "https://github.com/jackgreen/pdf-engine#readme",
43
- "directories": {
44
- "test": "tests"
45
- },
46
25
  "dependencies": {
47
- "pako": "^1.0.11",
48
- "tslib": "^1.14.1",
49
- "undici-types": "^8.3.0"
26
+ "pdf-lib": "^1.17.1"
50
27
  }
51
28
  }
package/pkg/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Jack Green
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/pkg/package.json CHANGED
@@ -1,17 +1,16 @@
1
1
  {
2
- "name": "pdf_engine",
3
- "type": "module",
4
- "version": "1.0.10",
2
+ "name": "pdf-engine",
3
+ "collaborators": [
4
+ "Jack Green"
5
+ ],
6
+ "description": "Rust/WASM PDF merge and split",
7
+ "version": "1.0.0",
8
+ "license": "MIT",
5
9
  "files": [
6
10
  "pdf_engine_bg.wasm",
7
11
  "pdf_engine.js",
8
- "pdf_engine_bg.js",
9
12
  "pdf_engine.d.ts"
10
13
  ],
11
14
  "main": "pdf_engine.js",
12
- "types": "pdf_engine.d.ts",
13
- "sideEffects": [
14
- "./pdf_engine.js",
15
- "./snippets/*"
16
- ]
15
+ "types": "pdf_engine.d.ts"
17
16
  }
@@ -1,14 +1,17 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
3
 
4
- export function extract_text(buffer: Uint8Array): string;
4
+ /**
5
+ * Merge multiple PDFs into a single PDF.
6
+ * Input: js_sys::Array of Uint8Array (each a valid PDF byte stream).
7
+ * Returns raw bytes of the merged PDF (to be wrapped as Uint8Array by wasm).
8
+ */
9
+ export function merge(pdfs: Array<any>): Uint8Array;
5
10
 
6
- export function get_page_count(buffer: Uint8Array): number;
7
-
8
- export function get_pdf_info(buffer: Uint8Array): string;
9
-
10
- export function init(): void;
11
-
12
- export function merge(pdf_buffers: Array<any>): Uint8Array;
13
-
14
- export function split(buffer: Uint8Array, pages: Array<any>): Array<any>;
11
+ /**
12
+ * Split a PDF by page ranges (inclusive, 0-indexed).
13
+ * pdf: Uint8Array of valid PDF bytes.
14
+ * ranges: Array of Uint8Array, each 8 bytes = [start_u32_le, end_u32_le].
15
+ * Returns Array of Uint8Array, one per range.
16
+ */
17
+ export function split(pdf: Uint8Array, ranges: Array<any>): Array<any>;
package/pkg/pdf_engine.js CHANGED
@@ -1,9 +1,122 @@
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";
4
2
 
5
- __wbg_set_wasm(wasm);
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;
6
122
  wasm.__wbindgen_start();
7
- export {
8
- extract_text, get_page_count, get_pdf_info, init, merge, split
9
- } from "./pdf_engine_bg.js";
Binary file
@@ -1,14 +1,8 @@
1
1
  /* tslint:disable */
2
2
  /* eslint-disable */
3
3
  export const memory: WebAssembly.Memory;
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];
4
+ export const merge: (a: any) => [number, number];
5
+ export const split: (a: any, b: any) => any;
10
6
  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;
13
7
  export const __wbindgen_free: (a: number, b: number, c: number) => void;
14
8
  export const __wbindgen_start: () => void;
package/dist/cli.d.ts DELETED
@@ -1,3 +0,0 @@
1
- #!/usr/bin/env node
2
- export {};
3
- //# sourceMappingURL=cli.d.ts.map
package/dist/cli.d.ts.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":""}
package/dist/cli.js DELETED
@@ -1,60 +0,0 @@
1
- #!/usr/bin/env node
2
- import { createRequire } from 'module';
3
- import pdfEngine from './index.js';
4
- const require = createRequire(import.meta.url);
5
- const USAGE = `pdf-engine — Rust/WASM PDF processing (v${require('../package.json').version})
6
-
7
- Usage:
8
- pdf-engine <command> [args]
9
-
10
- Commands:
11
- page-count <file> Print the page count of a PDF
12
- extract-text <file> Extract text from a PDF
13
- merge <file1> <file2> ... Merge PDFs into a single output
14
- split <file> <page1> ... Split a PDF by 0-based page indices
15
- help Print this message
16
- `;
17
- async function main() {
18
- const [cmd, ...args] = process.argv.slice(2);
19
- if (!cmd || cmd === '-h' || cmd === '--help' || cmd === 'help') {
20
- process.stdout.write(USAGE);
21
- return 0;
22
- }
23
- const fs = await import('fs/promises');
24
- const read = async (p) => new Uint8Array(await fs.readFile(p));
25
- await pdfEngine.init();
26
- switch (cmd) {
27
- case 'page-count': {
28
- const n = await pdfEngine.getPageCount(await read(args[0]));
29
- process.stdout.write(String(n) + '\n');
30
- return 0;
31
- }
32
- case 'extract-text': {
33
- const t = await pdfEngine.extractText(await read(args[0]));
34
- process.stdout.write(t + '\n');
35
- return 0;
36
- }
37
- case 'merge': {
38
- const out = await pdfEngine.merge(await Promise.all(args.map(read)));
39
- await fs.writeFile('merged.pdf', out);
40
- process.stdout.write(`wrote merged.pdf (${out.byteLength} bytes)\n`);
41
- return 0;
42
- }
43
- case 'split': {
44
- const [file, ...pages] = args;
45
- const out = await pdfEngine.split(await read(file), pages.map(Number));
46
- for (let i = 0; i < out.length; i++) {
47
- await fs.writeFile(`part-${i}.pdf`, out[i]);
48
- }
49
- process.stdout.write(`wrote ${out.length} part-*.pdf files\n`);
50
- return 0;
51
- }
52
- default:
53
- process.stderr.write(`unknown command: ${cmd}\n${USAGE}`);
54
- return 1;
55
- }
56
- }
57
- main().then((c) => process.exit(c)).catch((e) => {
58
- process.stderr.write(String(e) + '\n');
59
- process.exit(1);
60
- });
package/dist/index.d.ts DELETED
@@ -1,17 +0,0 @@
1
- export declare function initialize(): Promise<void>;
2
- export declare function getPageCount(buffer: Uint8Array): Promise<number>;
3
- export declare function extractText(buffer: Uint8Array): Promise<string>;
4
- export declare function merge(pdfBuffers: Uint8Array[]): Promise<Uint8Array>;
5
- export declare function split(buffer: Uint8Array, pages: number[]): Promise<Uint8Array[]>;
6
- export declare class PdfEngineImpl {
7
- constructor();
8
- private initialized;
9
- init(): Promise<void>;
10
- getPageCount(buffer: Uint8Array): Promise<number>;
11
- extractText(buffer: Uint8Array): Promise<string>;
12
- merge(pdfBuffers: Uint8Array[]): Promise<Uint8Array>;
13
- split(buffer: Uint8Array, pages: number[]): Promise<Uint8Array[]>;
14
- }
15
- declare const pdfEngine: PdfEngineImpl;
16
- export default pdfEngine;
17
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAIA,wBAAsB,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC,CAEhD;AAED,wBAAsB,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAEtE;AAED,wBAAsB,WAAW,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAErE;AAED,wBAAsB,KAAK,CAAC,UAAU,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAGzE;AAED,wBAAsB,KAAK,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAGtF;AAED,qBAAa,aAAa;IACtB,cAEC;IACD,OAAO,CAAC,WAAW,CAAS;IAEtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAK1B;IAEK,YAAY,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAGtD;IAEK,WAAW,CAAC,MAAM,EAAE,UAAU,GAAG,OAAO,CAAC,MAAM,CAAC,CAGrD;IAEK,KAAK,CAAC,UAAU,EAAE,UAAU,EAAE,GAAG,OAAO,CAAC,UAAU,CAAC,CAGzD;IAEK,KAAK,CAAC,MAAM,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,EAAE,GAAG,OAAO,CAAC,UAAU,EAAE,CAAC,CAGtE;CACJ;AAED,QAAA,MAAM,SAAS,eAAsB,CAAC;eACvB,SAAS"}
package/dist/index.js DELETED
@@ -1,49 +0,0 @@
1
- // PDF Engine TypeScript bindings for Rust/WASM PDF processing library
2
- import * as wasm from '../pkg/pdf_engine.js';
3
- export async function initialize() {
4
- await wasm.init();
5
- }
6
- export async function getPageCount(buffer) {
7
- return await wasm.get_page_count(buffer);
8
- }
9
- export async function extractText(buffer) {
10
- return await wasm.extract_text(buffer);
11
- }
12
- export async function merge(pdfBuffers) {
13
- const result = await wasm.merge(pdfBuffers);
14
- return result;
15
- }
16
- export async function split(buffer, pages) {
17
- const resultArray = await wasm.split(buffer, pages);
18
- return Array.from(resultArray).map(arr => arr);
19
- }
20
- export class PdfEngineImpl {
21
- constructor() {
22
- this.initialized = false;
23
- this.initialized = false;
24
- }
25
- async init() {
26
- if (!this.initialized) {
27
- await initialize();
28
- this.initialized = true;
29
- }
30
- }
31
- async getPageCount(buffer) {
32
- await this.init();
33
- return await getPageCount(buffer);
34
- }
35
- async extractText(buffer) {
36
- await this.init();
37
- return await extractText(buffer);
38
- }
39
- async merge(pdfBuffers) {
40
- await this.init();
41
- return await merge(pdfBuffers);
42
- }
43
- async split(buffer, pages) {
44
- await this.init();
45
- return await split(buffer, pages);
46
- }
47
- }
48
- const pdfEngine = new PdfEngineImpl();
49
- export default pdfEngine;