Clean up a scanned PDF's background
Cleaning up a scanned PDF means restoring the tonal range the scanner flattened: gray paper backgrounds are pushed back to white and faded text is darkened toward black. This page re-renders each page and applies a contrast stretch anchored at the 5th and 95th percentiles of the page's brightness histogram, which whitens photocopier gray and removes most bleed-through from the reverse side. An optional black-and-white mode binarizes the page completely for fax-crisp text. Processing happens entirely in your browser; output pages are images, so run OCR afterward if you need selectable text.
How it works
A dirty scan is mostly a problem of compressed tonal range. Real paper is not pure white — a typical photocopy background scans at around 85 percent brightness — and toner that has been through two or three copy generations is not pure black either, landing at maybe 25 percent. All the information is there; it is just squeezed into the middle of the scale. Cleanup is the act of stretching that squeezed range back out to the full black-to-white scale.
This tool renders each page to a bitmap with pdf.js, then builds a brightness histogram: a count of how many pixels sit at each of the 256 gray levels. From that histogram it reads two anchor points — the 5th percentile (the level below which the darkest 5 percent of pixels fall, effectively "the ink") and the 95th percentile (effectively "the paper"). Every pixel is then remapped linearly so the 5th-percentile level becomes black and the 95th-percentile level becomes white; anything lighter than the paper anchor — including faint bleed-through from the reverse side, which is almost always lighter than front-side ink — clips to pure white. Using percentiles rather than the absolute minimum and maximum is what makes the method robust: a single dust speck or a scanner edge shadow cannot skew the anchors, because 5 percent of the page has to be at least that dark before the black point moves.
The optional black-and-white mode goes one step further and binarizes: each pixel is compared against a threshold derived from the same histogram and forced to pure black or pure white with nothing in between. That is how fax machines and office copiers in text mode work, and it produces the sharpest possible letterforms and the smallest files — at the cost of destroying photographs and gray fills, which is why it is a switch and not the default.
The corrected bitmaps are re-encoded and assembled into a new PDF with pdf-lib at the original page dimensions. Because pages are rebuilt as images, any existing text layer is not carried over — after cleanup, use OCR PDF to make the document searchable again; the recognizer will actually perform better on the high-contrast version.
Illustrative example: a 12-page photocopied lease
Illustrative scenario, not a recorded benchmark. File sizes, timings and results depend on the input and device. Try the reproducible samples.
A 12-page lease agreement, photocopied twice before being scanned, arrives as a 6.1 MB PDF with a murky gray background at roughly level 218 of 255 and text sitting around level 70 — readable, but tiring, and ugly when printed because the printer reproduces all that background gray as toner. The tool processes the 12 pages in about 9 seconds on a mid-range laptop. The histogram anchors land at level 62 (5th percentile) and level 221 (95th percentile); after the stretch, the background is pure 255 white, the text sits near solid black, and the faint mirror image of clauses bleeding through from the reverse sides has clipped to white entirely.
The output file is 3.4 MB — smaller than the input, because pages that are mostly pure white compress far better than pages of mottled gray. Printed, it now uses toner only where there is ink. A second pass in black-and-white mode brings it down to 1.1 MB, which is the version that gets emailed; a follow-up OCR pass makes the emailed copy searchable.
Frequently asked questions
How do I clean up the gray background in a scanned PDF?
Apply a contrast stretch: measure the darkest and lightest tones actually present on each page and remap them so the paper tone becomes pure white and the ink becomes near black. This page does that automatically in your browser using the 5th and 95th percentiles of each page's brightness histogram, so no manual level adjustment is needed.
Will cleanup remove text showing through from the back of the page?
Usually, yes — bleed-through is much lighter than the front-side ink, so it sits above the 95th-percentile white point and gets pushed to pure white along with the paper tone. Very heavy bleed-through on thin paper may still leave faint traces, in which case the black-and-white mode removes it completely.
What does the black-and-white mode do differently?
Instead of stretching the gray range, it binarizes: every pixel is compared against a threshold and forced to either pure black or pure white, like a fax or photocopy. This gives the crispest possible text and the smallest files, but it destroys photos and gray shading, so use it only for text documents.
Is the text still selectable after cleanup?
No — output pages are rebuilt as images, so any text layer the file had is not carried over. If you need to search or copy the text, run the cleaned file through an OCR tool afterward; cleanup actually improves OCR accuracy because the recognizer sees higher-contrast letters.
Why do photocopies come out gray in the first place?
Scanners record the true brightness of the paper, and real paper — especially a photocopy of a photocopy — reflects far less light than pure white, so the background lands around 80 to 90 percent brightness instead of 100. Each generation of copying also lightens the ink, which is why old copies look washed out until the tonal range is stretched back.
Does my scan get uploaded to a server for processing?
No — every page is rendered, analyzed, and rebuilt by JavaScript on your own device, and the file never leaves your computer. That makes the tool safe for contracts, medical records, and other documents you would not send to a third-party server.