How I built free PDF tools that run fully in the browser (no uploads)
Most "free PDF tool" sites ask you to upload the file. That is a simple architecture: the browser posts a binary, a server runs Ghostscript or similar, and a download link comes back. It also means a contract, a bank sta
Most "free PDF tool" sites ask you to upload the file. That is a simple architecture: the browser posts a binary, a server runs Ghostscript or similar, and a download link comes back. It also means a contract, a bank statement or an ID scan sits on someone else's disk, at least for a while, and you pay for CPU every time a file is processed.
I did not want that for DailyToolLab. The PDF tools are static pages. The file is read with the File API, processed in the tab, and written back as a Blob download. There is no upload endpoint. The only fetch in the shared PDF kit is for a self-hosted WebAssembly engine, not for the user's document.
That choice is about privacy, but it is also about cost and speed. Once the page and the libraries have loaded, compression does not wait on a queue or an upload. A large file never leaves the device, so a slow uplink is irrelevant. The trade-off is that the user's phone or laptop is the server, with all the memory and font limits that implies.
Two libraries, two jobs
The core tools รขโฌโ merge, split, compress, images-to-PDF, PDF-to-JPG and the PDF editor รขโฌโ share one script. It lazy-loads two engines from our own origin:
-
pdf-lib 1.17.1 for reading and writing the PDF structure:
PDFDocument.load,copyPages,embedJpg/embedPng,embedFont,save. - Mozilla PDF.js 4.10.38 for rendering pages to a canvas. That is how thumbnails, PDF-to-JPG and the stronger compress modes work.
Both are vendored under /assets/vendor/, so the tab does not have to reach a third-party CDN to run them. PDF.js is configured with packed CMaps, the standard font data files, and isEvalSupported: false.
A third engine shows up only on Unlock PDF: qpdf compiled to WebAssembly (qpdf-wasm 0.3.1). Passwords stay in the input field. The tool makes one attempt per click; it does not guess. Certificate- or DRM-locked files are rejected, and files over 300 MB are refused. Encrypted files also fail in pdf-lib, so the other tools ask you to unlock first, then open the unlocked copy.
Compressing without a server
Compress PDF has four modes. The important split is "keep the page content" versus "redraw it as an image".
Basic never rasterises. It loads the file with pdf-lib and saves it again with useObjectStreams: true. Text stays selectable and searchable. Savings are whatever you get from tightening the file structure; already-efficient, mostly-text exports often do not shrink, and the UI says so.
Recommended and Strong do the opposite. Each page is rendered with PDF.js at a fixed resolution, encoded as a JPEG, and placed on a new page that keeps the original size in PDF points:
- Recommended: 144 dpi, JPEG quality
0.72 - Strong: 96 dpi, JPEG quality
0.55
That is the same idea as "print to image", and the honest cost is that text can no longer be selected or searched, and links do not survive. The tool warns about this when it happens.
Target size aims for 100 KB, 200 KB, 500 KB, 1 MB, or a custom limit between 20 KB and 50,000 KB:
- Try Basic first, so a file that already fits keeps its selectable text.
- If it is still over the target, walk a fixed ladder of raster settings, from 150 dpi at 80% JPEG quality down through 144, 120, 100, 96 and 72 dpi, with quality falling as far as 50%.
- Keep the first result that fits. If none do, keep the smallest attempt and say so รขโฌโ then suggest splitting the file or removing pages, rather than pretending the target was met.
A cap of eight total attempts, including the Basic pass, keeps a phone from grinding through every step.
The raster loop itself is small. A shortened version of the production code:
async function compressRaster(PDFLib, srcBytes, dpi, quality) {
const src = await pdfjs.getDocument({ data: srcBytes.slice() }).promise;
const out = await PDFLib.PDFDocument.create();
const canvas = document.createElement("canvas");
const ctx = canvas.getContext("2d");
for (let i = 1; i <= src.numPages; i++) {
const page = await src.getPage(i);
const v1 = page.getViewport({ scale: 1 });
const scale = safeScale(v1.width, v1.height, dpi / 72);
const vp = page.getViewport({ scale });
canvas.width = Math.max(1, Math.floor(vp.width));
canvas.height = Math.max(1, Math.floor(vp.height));
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.height);
await page.render({ canvasContext: ctx, viewport: vp }).promise;
const blob = await new Promise((resolve, reject) => {
canvas.toBlob((b) => (b ? resolve(b) : reject()), "image/jpeg", quality);
});
const jpg = await out.embedJpg(new Uint8Array(await blob.arrayBuffer()));
out.addPage([v1.width, v1.height])
.drawImage(jpg, { x: 0, y: 0, width: v1.width, height: v1.height });
page.cleanup();
}
src.destroy();
canvas.width = canvas.height = 0;
return out.save({ useObjectStreams: true });
}
safeScale exists because canvases are not free. It caps a render at 16 million pixels (a nod to iOS Safari's ~16.7 megapixel limit) and at 8,000 px on the long side. After each page the loop yields to the event loop, cleans up the PDF.js page, and at the end it destroys the document and zeroes the canvas so the next attempt does not stack bitmaps in memory.
The limits you hit in a tab
Memory. Merge, split and Basic compression hold the source as a Uint8Array and build a second document in pdf-lib. Raster modes add a full-page canvas and a JPEG for every page, one at a time. Compress PDF has no general file-size cap (Unlock PDF stops at 300 MB; Word to PDF at 60 MB). On a low-memory phone, a huge scan can still fail.
Fonts. Added text in the editor uses pdf-lib's standard fonts: Helvetica, Helvetica-Bold, Times-Roman and Courier. Those cover Latin letters, digits and common symbols. They do not cover Urdu, Arabic, Hindi, Chinese or emoji; widthOfTextAtSize throws, and the editor asks you to remove those characters. Existing page content is copied, not redrawn, so original text and embedded fonts stay as they were.
What raster throws away. If the job is "get this scan under 200 KB for a form", redrawing pages as JPEGs is usually the right tool. If the job is "keep this contract searchable", it is the wrong one. Target size tries Basic first for that reason.
Editing is not Word. The editor rotates, deletes and reorders pages, and it can overlay real PDF text. It does not rewrite words already in the file. A white box behind new text can hide a typo, but the old content is still in the file, so that is the wrong way to remove confidential information.
What I would do the same way
Self-host the engines. Lazy-load them only when a tool actually runs. Be explicit when a mode destroys text. Treat "did not reach the target" as a first-class result, not a failure to paper over. And measure canvas size against real device limits, not against whatever looks sharp on a desktop monitor.
Client-side PDF tools will not replace a headless Ghostscript box for 2,000-page batch jobs. For the everyday file you do not want to upload, a tab is enough. The compress and editor pages are live if you want to try this approach, or to read the same libraries from /assets/vendor/ yourself.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes โ full credit and traffic to the original publisher.