When you build a browser-based photo restoration app like Mended Photo, you expect to support JPEG, PNG, WebP, iPhone HEIC, and 16-bit flatbed TIFF scans.

What surprises almost every web engineer the first week real US desktop users start uploading shoebox family albums is how many people scan photos as .pdf files.

Nearly every home HP, Epson, and Canon all-in-one printer-scanner defaults its "Scan" button to PDF. To a non-technical user digitizing a grandmother's portrait, Scan_0004.pdf is their photo. If your dropzone rejects PDFs or asks them to convert files first, you lose them on the spot.

Here is how we added pure browser-local scanner PDF image extraction without sending multi-megabyte scanner dumps to a serverless backend, plus a related 4K canvas payload bug we caught in production yesterday.

Why Not Render PDFs Through PDF.js Canvas?

The textbook browser approach to reading a PDF is loading pdfjs-dist with its worker bundle and rasterizing page 1 onto a <canvas>. That works for text documents, but for scanned family photos it has three real downsides:

  1. Bundle size and worker complexity: Shipping the full PDF.js worker adds megabytes and CSP/worker headaches.
  2. Resampling loss: Rasterizing a PDF page at a guessed scale (say 2x) either downscales a crisp 600-DPI embedded scan or blurs a native JPEG stream through an extra decode/scale/re-encode pass.
  3. White scanner-bed borders: Home scanners often embed a clean photo XObject inside an 8.5x11" page. Extracting the embedded image stream directly gives you the raw scanner bitmap with zero resampling overhead.

Extracting Embedded Image XObjects with pdf-lib + fflate

In PDFs produced by consumer scanners, the photograph almost always lives inside /Resources -> /XObject as a PDFRawStream with /Subtype /Image. Instead of rendering the whole PDF page, we inspect the page's XObject dictionary in TypeScript using pdf-lib and select the largest image stream by pixel area (width * height).

Two filters cover ~98% of real-world home scanner PDFs:

Case 1: /Filter /DCTDecode (Embedded JPEG)

The stream bytes are already a valid JPEG file. You can wrap rawStream.contents directly into a Blob([bytes], { type: 'image/jpeg' }) and hand it straight to createImageBitmap(). Zero decompression overhead, zero quality loss.

Case 2: /Filter /FlateDecode (Zlib-Compressed Raw Pixels)

Many Canon/Epson scanners and macOS Preview exports write zlib-compressed raw RGB, Grayscale, or CMYK pixel rows (often with PNG-style row predictors like /Predictor 12 Up or /Predictor 15 Optimum).

The decoding pipeline in pure TypeScript is surprisingly compact:

  1. Inflate the raw stream bytes with fflate.unzlibSync(bytes).
  2. Check /DecodeParms -> /Predictor. If predictor >= 10, reverse the PNG filter byte at the start of every scanline (None, Sub, Up, Average, Paeth) into raw packed pixel bytes.
  3. Inspect /ColorSpace (DeviceRGB, DeviceGray, or DeviceCMYK) and /BitsPerComponent (8 or 16) and expand into an RGBA Uint8ClampedArray on an offscreen <canvas>.
  4. Export a clean normalized bitmap ready for preview and restoration.
// Simplified flow from our client-side PDF scanner extractor const inflated = unzlibSync(rawBytes); const rawPixels = predictor >= 10 ? unfilterPngRows(inflated, width, height, channels, bytesPerSample) : inflated; const rgba = expandToRgba(rawPixels, width, height, colorSpace, bitsPerComponent); ctx.putImageData(new ImageData(rgba, width, height), 0, 0); 

Because every byte stays in the user's browser tab, a 12 MB scanner PDF never hits edge request limits, and the user gets immediate magic-byte detection even if they accidentally renamed a .heic or .pdf file to .jpg.

Bonus Production War Story: The 4K Guided Brush HTTP 413 Bug

Yesterday we caught a subtle payload bug in our guided brush retouch canvas (where users paint a mask over one specific torn corner or faded dress color to refine a restored portrait without re-rolling the whole face).

Here is what happened:

  • Restored archival masters are crisp 4K PNGs (up to 4800px) so they print cleanly at 300 DPI (8x10" and 11x14").
  • When a user opened the brush retouch tool on an already-unlocked 4K master, the server loaded the full 4K PNG master, the original scan, and the painted mask PNG, base64-encoded all three into a single JSON request, and crossed the upstream 10 MB request body ceiling, triggering HTTP 413 on the largest masters.

The fix took two clean changes:

  1. On the client canvas, send a compact 1600px JPEG baseSnapshot alongside the painted mask rather than forcing the server to re-serialize an 8 MB 4K PNG into JSON.
  2. On the server edge handler, enforce an explicit 8.2 MB total base64 payload budget before calling the image pipeline, gracefully dropping optional secondary reference attachments if a request ever approaches the ceiling while keeping the full 4K archival master intact in storage.

If you are working with old prints yourself or curious how much resolution your scans actually hold before printing, try our free 100% browser-local 300-DPI Old Photo Print Size Calculator, or inspect our enlargement and 300-DPI framing guide.

Happy to answer questions in the comments about pdf-lib XObject quirks, CMYK conversion, or client-side TIFF/HEIC normalization!