Move PDF operations from a server endpoint to a WebAssembly module running in the browser. PDFluent compiles to WASM. No server round-trips for basic PDF operations.
cargo add pdfluentNot every PDF operation belongs in the browser. Operations that work well in WASM: form filling, text extraction, annotation, metadata editing, and basic PDF creation. Operations that should stay server-side: high-security signing, server-only data injection, and compliance workflows requiring an audit trail.
// Current: every PDF operation hits your server
// POST /api/pdf/fill-form
// POST /api/pdf/extract-text
// POST /api/pdf/add-annotation
// POST /api/pdf/merge
//
// Round-trip latency: 200-800ms per operation
// Server load: scales with user count
// Offline use: impossible// Target: common operations run in the browser
// WASM module loaded once, runs locally
//
// Round-trip latency: 0ms (runs in tab)
// Server load: only for signing and compliance
// Offline use: fully supportedPDFluent compiles to WebAssembly using wasm-pack. The output is a ~6 MB .wasm file (~2 MB Brotli-compressed over the wire) plus a JavaScript glue module. Add the wasm target to your Rust toolchain and run wasm-pack build to produce an npm-compatible package.
// Server endpoint (Node.js / Express example)
app.post('/api/pdf/fill-form', async (req, res) => {
const { pdfBytes, fields } = req.body;
// Server receives PDF bytes from client,
// processes them, returns filled PDF.
// Every user action requires a network round-trip.
const filled = await fillFormOnServer(pdfBytes, fields);
res.send(filled);
});# Build PDFluent for WASM
rustup target add wasm32-unknown-unknown
cargo install wasm-pack
wasm-pack build --target web --release
# Output: pkg/pdfluent_bg.wasm (~6 MB, ~2 MB Brotli-compressed)
# pkg/pdfluent.js (glue module)Import the wasm-pack output as an ES module in your frontend code. Initialize the WASM binary once on page load, then call PDFluent operations directly in the browser. Replace fetch() calls to your server endpoint with direct WASM calls on the PDF ArrayBuffer.
// Before: round-trip to server
async function fillForm(pdfBytes, fields) {
const response = await fetch('/api/pdf/fill-form', {
method: 'POST',
body: JSON.stringify({ pdfBytes: Array.from(pdfBytes), fields }),
headers: { 'Content-Type': 'application/json' },
});
return new Uint8Array(await response.arrayBuffer());
}// After: runs in the browser via WASM
import init, { fill_form_fields } from './pkg/pdfluent.js';
await init(); // load .wasm once at startup
async function fillForm(pdfBytes, fields) {
// Runs locally — no network, no server
return fill_form_fields(pdfBytes, fields);
}