DEV Community

akashkore5
akashkore5

Posted on Originally published at editpdfkit.com

Zero-Upload PDFs: Building a Privacy-First Editor with WebAssembly

Zero-Upload PDFs: Building a Privacy-First Editor with WebAssembly

If you’ve ever used an online PDF tool, you’ve likely experienced that moment of hesitation. You click "Upload," and your sensitive document—a contract, a medical record, or a financial statement—begins its journey across the public internet to a remote server.

Even if the service advertises "files deleted after 1 hour," the damage is already done. Your data has traversed the network, sat in a temporary cloud bucket, and been processed by a runtime you have no visibility into.

As engineers, we know that "temporary" is a dangerous word. Logs, swap files, and infrastructure snapshots can persist long after the primary file is gone. This realization drove us to build EditPDFKit with a strict local-first architecture. We didn't just want to promise privacy; we wanted to make data exfiltration technically impossible by keeping all processing within the browser's sandbox.

The Hidden Cost of "Privacy" in Traditional Architectures

The industry standard for PDF manipulation typically relies on server-side libraries like iText (Java), PyPDF2 (Python), or wrappers around Poppler. While these libraries are robust, they inherently require a client-server round trip.

Let’s look at the lifecycle of a simple "Merge PDF" request in a traditional setup:

  1. Client: POSTs two 10MB PDF files to /api/merge.
  2. Network: 20MB of data travels over the wire.
  3. Server: Receives the stream and writes it to a temporary /tmp directory.
  4. Processor: A worker process loads files into memory, merges them, and outputs a new 20MB file.
  5. Storage: The new file is often moved to object storage (like AWS S3) to generate a download link.
  6. Cleanup: A background job eventually deletes the files.

The "privacy cost" here isn't just the risk of a breach. It’s the fact that your PII (Personally Identifiable Information) has now entered the scope of the service provider’s compliance requirements (GDPR, HIPAA, CCPA).

By shifting the compute to the client, we eliminate this entire chain. The browser handles the heavy lifting, and the data never leaves the user's RAM.

Server-Side vs. Client-Side Flow

sequenceDiagram
    participant U as User Browser
    participant S as Remote Server
    participant DB as Cloud Storage

    Note over U, DB: Traditional Server-Side Model
    U->>S: Upload Sensitive.pdf (POST)
    S->>DB: Store temporary copy
    S->>S: Process PDF (CPU/RAM intensive)
    S->>U: Send Download Link
    Note over U, DB: EditPDFKit Local-First Model
    U->>U: Load PDF into WebWorker
    U->>U: Process via WebAssembly/JS
    U->>U: Generate Blob URL
    Note right of U: 0 bytes transferred to server

Powering Local-First Tools: WebAssembly and Modern JS

For years, client-side PDF manipulation was limited to basic viewing. Complex operations—such as modifying the cross-reference table (xref), merging encrypted streams, or subsetting fonts—required more compute power and lower-level memory management than standard JavaScript could efficiently provide.

The shift toward local-first tooling was enabled by two key factors: the maturity of the pdf-lib ecosystem and the raw performance of WebAssembly (Wasm).

At EditPDFKit, we utilize these technologies to handle PDF structures directly in the browser. When you drop a file into the editor, we use the File API to get an ArrayBuffer. This buffer is parsed into a document object model that exists solely in the browser's allocated memory.

Code Example: Local PDF Merging

Here is a simplified look at how we handle a merge operation without a backend. This code runs entirely in the user's browser thread (or, ideally, a Web Worker).

import { PDFDocument } from 'pdf-lib';

async function mergePDFs(pdfBuffers) {
  // Create a new PDF document in the client's memory
  const mergedPdf = await PDFDocument.create();

  for (const buffer of pdfBuffers) {
    // Load the PDF from the ArrayBuffer (no network request)
    const pdf = await PDFDocument.load(buffer);

    // Copy pages from the source to the new document
    const copiedPages = await mergedPdf.copyPages(
      pdf, 
      pdf.getPageIndices()
    );

    copiedPages.forEach((page) => mergedPdf.addPage(page));
  }

  // Serialize the PDF to bytes (Uint8Array)
  const pdfBytes = await mergedPdf.save();

  // Create a local Blob URL for the user to download
  const blob = new Blob([pdfBytes], { type: 'application/pdf' });
  return URL.createObjectURL(blob);
}
Enter fullscreen mode Exit fullscreen mode

In this snippet, the pdfBuffers are sourced directly from an <input type="file">. The resulting ObjectURL is a reference to the browser's internal memory. The data transfer to our servers is exactly zero.

The Engineering Trade-offs of Client-Side Processing

While the privacy benefits are absolute, moving PDF processing to the client isn't a free lunch. We have to account for the constraints of the user's environment.

1. Memory Pressure

PDFs can be massive. A 500MB technical manual can easily crash a mobile browser tab if you attempt to load the entire document into a single Uint8Array. We solve this by using streaming buffers where possible and encouraging users to perform atomic operations (like splitting) before heavy editing.

2. CPU Bottlenecks

Operations like OCR (Optical Character Recognition) or complex flattening are CPU intensive. While JavaScript engines are fast, they aren't C++ fast. This is where WebAssembly shines. By compiling heavy-duty C or Rust libraries to Wasm, we can achieve near-native speeds for tasks like image compression within the PDF.

3. Browser Compatibility

We rely on modern browser APIs (File System Access API, Web Workers, and SharedArrayBuffer). While this covers 95%+ of modern users, it does mean that legacy enterprise browsers (IE11, we're looking at you) are intentionally unsupported to maintain the security posture.

Why "Trust Us" is a Bad Security Policy

In the software world, the most secure data is the data that never leaves your machine.

Top comments (0)