DEV Community

How I Built PDF and Image Tools That Run 100% in the Browser

Most "free PDF tools" work the same way: you upload a file, a server processes it, you download the result. That's fine for a menu or a flyer, less fine for a passport scan or a salary slip.

For FreeAIEdits I wanted the opposite: the file never leaves the user's device. Every PDF and image tool runs in the browser. Here's how the main pieces work, and the trade-offs I hit.

The stack

  • pdf-lib for creating and editing PDFs (merge, split, rotate, delete pages, page numbers, signatures)
  • pdf.js for reading PDFs: rendering pages to a canvas and extracting text
  • docx (the npm library) for writing Word files
  • @imgly/background-removal with onnxruntime-web for AI background removal
  • Plain <canvas> for image compression, resizing and conversion

Libraries are loaded lazily from a CDN only when a tool needs them, so the home page stays light.

Merging PDFs is the easy part

With pdf-lib, merging is just copying pages from one document into another:

const out = await PDFLib.PDFDocument.create();
for (const file of files) {
  const src = await PDFLib.PDFDocument.load(await file.arrayBuffer());
  const pages = await out.copyPages(src, src.getPageIndices());
  pages.forEach(p => out.addPage(p));
}
const blob = new Blob([await out.save()], { type: 'application/pdf' });

Split, rotate, delete and extract pages are all variations of this.

Compressing PDFs: honest trade-offs

There's no magic "make it smaller" button inside pdf-lib. I ended up with three levels:

  • Light: load and re-save with pdf-lib. This keeps text selectable and often trims some bloat, but won't shrink scans much.
  • Medium / Strong: render each page with pdf.js to a canvas, export it as a JPEG at a chosen scale and quality, and build a new PDF from those images.
const vp = page.getViewport({ scale: 1.6 }); // medium
canvas.width = vp.width;
canvas.height = vp.height;
await page.render({ canvasContext: ctx, viewport: vp }).promise;
const jpg = await out.embedJpg(await (await toBlob(canvas, 'image/jpeg', 0.65)).arrayBuffer());
out.addPage([base.width, base.height]).drawImage(jpg, { x: 0, y: 0, width: base.width, height: base.height });

This shrinks scanned PDFs dramatically, but the text is no longer selectable, so the UI says that clearly. Being upfront about trade-offs matters more than a fancy progress bar.

PDF to Word without a server

pdf.js gives you text items with positions and font sizes. I group them into lines and paragraphs, then guess headings by comparing a paragraph's font size to the page's body size (roughly: 1.8x and above is H1, 1.45x is H2, anything larger than body is H3).

Bullets are detected from leading characters like •, - or ▪. The docx library then writes real Word paragraphs, headings and lists.

Scanned pages have no text layer at all, so those pages are embedded as images and the user is told to run OCR first. It handles normal documents well; heavy multi-column or table layouts still need tidying, and I'd rather say that than pretend otherwise.

AI background removal on the device

The background remover runs an ONNX segmentation model in the browser via @imgly/background-removal. Two lessons:

  • Pin your versions. A CDN build that resolved to a newer onnxruntime-web broke silently. Pinning the runtime version fixed it.
  • AI masks need a human touch-up. Hair and edges are never perfect, so I added a simple mask brush (restore / erase) on top of the AI result.

Hitting an exact file size

Forms love rules like "photo must be under 50 KB". Image compression does a small search over JPEG quality (and scale if needed) until the output lands just under the target. A 5.1 MB phone photo comes out around 49.6 KB.

What I'd tell anyone building this

  • Browsers are now fast enough for most document chores. Server-side is optional for a lot of everyday work.
  • Lazy-load heavy libraries per tool.
  • Be explicit in the UI about what each option does to the file.
  • "Your file never leaves your device" is a feature people actually notice.

If you want to poke at it, everything is free with no sign-up at freeaiedits.com. Happy to answer questions about any of the pieces in the comments.

Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.