How to shrink a PDF to a target size in the browser: render pages, split a byte budget, retry three times
Court portals, job sites and university forms often cap uploads at a number like 200 KB or 2 MB. Most PDF compressors give you a quality slider and leave you to guess. If you know the target, you can make the code hit it
Court portals, job sites and university forms often cap uploads at a number like 200 KB or 2 MB. Most PDF compressors give you a quality slider and leave you to guess. If you know the target, you can make the code hit it.
Here is how the Compress PDF tool on Toolsy Hub does it, all in the browser with pdf-lib and pdf.js.
Two modes, because they trade different things
Light re-saves the file with pdf-lib and object streams turned on. Text stays selectable and nothing is re-encoded, so the saving is small. If the PDF has pictures that are the real weight, light mode will not help.
Images renders every page to a JPEG with pdf.js and rebuilds the PDF from those pictures. A scanned document shrinks a lot. The cost is that text becomes part of the picture, so it is no longer selectable. The tool says this before you pick it.
Render once at the sharpest setting
There are three presets: 150 dpi at JPEG quality 0.8, 120 dpi at 0.65, and 96 dpi at 0.5. When you type a target size, the tool ignores the preset and starts every page at the sharpest one, 150 dpi at 0.8. It lowers quality only as far as the target needs. Starting low and going up would leave a sharp document blurry for no reason.
Split the byte budget by pixel area
If the first PDF is already under the target, you are done. If not, the target in bytes is divided across the pages in proportion to each page's pixel area (width times height). A big page gets a bigger share than a small one, so one large scan does not eat the whole budget.
Each page image is then recompressed to its share with a quality search, and the PDF is rebuilt.
Retry, but only three times
Page shares add up, but PDF structure adds overhead on top of the images, so the rebuilt file can land a little over. The tool then tightens the budget by target / size * 0.95, never by more than half in one step, and tries again. Three attempts at most. If it still misses, it hands back the smallest result and says the target was not reached, rather than looping or silently returning a file that breaks your limit.
The "say so" part matters. A file that is 30 KB over a 200 KB limit gets rejected by the portal, and the person finds out only after the upload fails.
What stays in the browser
Nothing is sent to a server for this. pdf-lib is loaded on demand (about 180 KB) so the page itself stays light. Files you add stay in your browser; Google Analytics counts visits to the site.
Try it: https://www.toolsy-hub.com/tools/compress-pdf. For a fixed limit there is a page with the size already set: https://www.toolsy-hub.com/tools/compress-pdf/200kb.
What I would improve: the retry factor is a heuristic. A proper search over per-page quality would hit the target more closely on files with mixed pages. If you have a PDF that misses its target, I would like to hear about it.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.