The problem with JS-only PDF libraries
pdf-lib is great — it's the go-to for PDF manipulation in Node.js and the browser. But when you hit larger files or need text extraction, the pure-JavaScript approach shows its limits. Page counting scans the entire document. Merging is slow. And there's no built-in text extraction from compressed content streams.
I needed something faster for a project, so I built a PDF engine in Rust and compiled it to WebAssembly.
Why Rust + WASM?
Rust gives you:
- Memory safety without a garbage collector
- Zero-cost abstractions — the compiler optimizes the hot paths
- A proper PDF parser (lopdf) that understands the spec, not just regex hacks
WebAssembly lets you run that Rust code in the browser or Node.js without a server. The binary is ~280 KB total — smaller than pdf-lib's bundle.
What it does
The API is dead simple:
import pdfEngine from '@jackgreen2018/pdf-engine-wasm';
await pdfEngine.init();
// Fast page count
const pages = await pdfEngine.getPageCount(buffer);
// Text extraction from compressed streams
const text = await pdfEngine.extractText(buffer);
// Merge PDFs — 4x faster than pdf-lib
const merged = await pdfEngine.merge([pdf1, pdf2, pdf3]);
// Split by page numbers
const parts = await pdfEngine.split(buffer, [1, 3, 5]);
All processing happens in the browser. No uploads, no server, no telemetry. Your PDFs never leave your machine.
Benchmarks
| Operation | pdf-engine-wasm | pdf-lib |
|---|---|---|
| Page count (10 pages) | 0.61 ms | 1.29 ms |
| Merge (2 files) | 0.93 ms | 3.68 ms |
| Split (pages [0,1]) | 0.18 ms | 1.72 ms |
| Text extraction | 0.20 ms | N/A |
Text extraction is unique — pdf-lib doesn't support it at all. pdf-engine-wasm parses the actual PDF content streams, including hex-encoded and FlateDecode-compressed text.
Try it
I built a live demo where you can test all four operations right in your browser:
https://tools.jackgreen.top/mission-to-build-a-rust-wasm-pdf-engine-npm-package-for/
No signup. No upload. Just drop a PDF and see the results.
Install it
npm install @jackgreen2018/pdf-engine-wasm
Or grab the source: https://github.com/jackgreen/pdf-engine-wasm
MIT licensed. Contributions welcome.
The CLI
There's also a CLI for Node.js environments:
npx @jackgreen2018/pdf-engine-wasm page-count document.pdf
npx @jackgreen2018/pdf-engine-wasm merge file1.pdf file2.pdf
npx @jackgreen2018/pdf-engine-wasm split document.pdf 1 3 5
Perfect for build scripts, CI pipelines, or batch processing.
Why this matters
PDF is everywhere — invoices, reports, contracts, receipts. Most tools that handle PDFs either:
- Upload your files to a server (privacy nightmare)
- Use slow JavaScript libraries (frustrating UX)
- Charge subscription fees (unacceptable)
This is option 4: fast, private, and free.
Built with Rust, WebAssembly, and a lot of patience reading the PDF spec.
Top comments (0)