Dev.to WebDev 🛠 Dev 👁 0 📖 2 min read

How I Built a Browser-Local Target-KB PDF Compressor with Binary Search & Grayscale Mode (Zero Uploads)

Why generic "Low / Medium / High" PDF compressors fail strict visa & exam portal caps—and how to hit exact KB/MB limits locally using PDF.js and Canvas. When you upload a scanned application packet to an immigration po

Why generic "Low / Medium / High" PDF compressors fail strict visa & exam portal caps—and how to hit exact KB/MB limits locally using PDF.js and Canvas.

When you upload a scanned application packet to an immigration portal like IRCC Canada (4 MB limit) or attach a mark sheet to a recruitment portal (200 KB–500 KB limit), generic "Basic vs. Strong" PDF compressors often miss the mark.

Either the file comes out at 4.15 MB and still gets rejected, or the compressor crushes it down to 90 KB and makes every stamp, signature, and line of text unreadable.

Even worse, most online PDF compressors upload your passport scans, bank statements, or tax returns to a remote server before compressing them.

To fix both problems, I built the Target-KB PDF Compressor on DailyEZTools—running 100% inside the browser tab with zero server uploads.

1. Binary Search Over Render Scale & JPEG Quality

Instead of guessing a single compression ratio, the user enters their exact ceiling (for example, 3800 KB for a 4 MB portal limit, or 250 KB for an exam upload).

For scanned or image-heavy PDFs, the browser renders each page onto an offscreen HTML5 <canvas> via PDF.js and runs a bounded binary search across JPEG quality (0.35 to 0.92) and DPI scale so the final assembled PDF lands just below the user's target KB ceiling—preserving the highest possible sharpness.

async function compressCanvasToTargetBytes(canvas, targetBytes, grayscale) {
  if (grayscale) applyGrayscaleLuma(canvas);

  let low = 0.32, high = 0.92, bestBlob = null;
  for (let step = 0; step < 6; step++) {
    const mid = (low + high) / 2;
    const blob = await new Promise(res => canvas.toBlob(res, "image/jpeg", mid));
    if (blob.size <= targetBytes) {
      bestBlob = blob;
      low = mid; // Try higher quality while staying under the ceiling
    } else {
      high = mid; // Too large, step quality down
    }
  }
  return bestBlob || await new Promise(res => canvas.toBlob(res, "image/jpeg", 0.30));
}

2. Why Grayscale Mode Saves 40–65% on Scanned Forms

When you photograph or scan a black-and-white document under indoor lighting, the RGB channels store warm yellow and blue paper noise. By converting pixels to luminance grayscale (Y = 0.299R + 0.587G + 0.114B) before JPEG encoding, chroma noise disappears—cutting file size by 40–65% while keeping text edges crisp.

If you are starting from raw phone photos of paper documents rather than an existing PDF, you can also use our Images & Flatbed Scan to PDF Studio to lift paper shadows into pure white A4 pages before compressing.

3. Built-In 2× Sharpness Check & Zero-Upload Proof

Before downloading, the workspace lets you inspect fine print at 2× zoom so you never submit an illegible document.

And because the entire pipeline runs locally in browser memory:

  1. Open DevTools (F12) → Network
  2. Select your PDF in the Target-KB PDF Compressor
  3. Verify that 0 bytes of your file are ever uploaded to a server.

Preparing documents for a specific visa or exam portal? Check out our verified IRCC Canada 4 MB / 5 MB Upload Guide or browse the full Portal Upload Specifications Directory.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.