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. 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…
Courtrooms, job websites, and university forms frequently set PDF upload limits, such as 200 KB or 2 MB. PDF compression tools usually provide a quality slider, leaving users to guess the best setting. However, if you know the desired file size, you can directly achieve it. The Compress PDF tool on Toolsy Hub accomplishes this entirely in the browser using pdf-lib and pdf.js. Two modes are available: Light and Image.
In Light mode, the PDF is re-saved using pdf-lib, keeping text selectable and minimizing file size without changing the encoding. This mode is suitable when images are not the primary cause of the large file size. However, if images are the main contributor to the file size, Light mode may not be effective. In Image mode, each page is rendered as a JPEG by pdf.js and then rebuilt into a new PDF.
This approach is particularly useful for scanned documents, as it significantly reduces the file size. The trade-off is that text becomes part of the image and is no longer selectable.
The tool offers three presets: 150 dpi at JPEG quality 0.8, 120 dpi at 0.65, and 96 dpi at 0.5. When a target size is specified, the tool disregards these presets and initializes every page at the highest quality (150 dpi at 0.8). It only reduces the quality until it meets the target size. Starting at a higher quality and then reducing it would result in a sharp document becoming blurry unnecessarily.
If the initial PDF is already under the target size, the process stops. Otherwise, the target byte budget is divided among the pages based on their pixel area (width multiplied by height). Pages with larger areas receive a larger share of the budget to prevent a single big scan from consuming the entire limit. Each page image is then recompressed according to its allocated share, using a quality search, and the PDF is rebuilt.
The tool attempts to adjust the budget one more time by multiplying target / size * 0.95, ensuring it does not exceed a half-size reduction in a single step. It tries up to three times. If the target size is still not achieved, the tool returns the smallest file it could produce and informs the user that the target was not reached.
This feature is crucial, as a file that exceeds the 200 KB limit will be rejected by the portal, and the user will only learn of the failure upon attempting the upload. Everything happens within the browser; no files are sent to a server. The pdf-lib library is loaded on demand (approximately 180 KB), keeping the page lightweight.
The files you upload remain in your browser. The site uses Google Analytics to track visits. You can try it yourself at https://www.toolsy-hub.com/tools/compress-pdf, or use the pre-set limit page at https://www.toolsy-hub.com/tools/compress-pdf/200kb.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.