Under the Hood: The Architecture of PDF Page Cropper
Cloud “crop PDF online” farms upload the document, rasterize or rewrite boxes on a worker, and often cache the file for a download link. That is a document-privacy liability: invoices and IDs sit on someone else’s disk. This cropper never posts the PDF. It loads the bytes with pdf-lib in the tab, shrinks CropBox, MediaBox, BleedBox, TrimBox, and ArtBox by a margin in points, and saves a new file. Viewers that ignore CropBox still honor MediaBox, so the trim is real, not a hidden crop that print shops ignore.
Preview uses pdf.js on a canvas with a dashed inset so you can see the margin before download. No OCR, no page-extraction confusion: crop is margins on every page, not “keep pages 2–4.”
Memory Allocation & Browser Threading Mechanics
file.arrayBuffer() copies the PDF into a Uint8Array so pdf.js cannot detach the File buffer. pdf-lib parses objects on the main thread. Crop is a loop over getPages() mutating four floats per page—cheap compared to decode. Preview render is the heavier blit; it is one page at a time. Save allocates a new byte array for the rewritten xref.
Algorithmic Deep-Dive: Code Snippet & Logical Analysis
const box = page.getCropBox(); const w = Math.max(1, box.width - margin * 2); const h = Math.max(1, box.height - margin * 2); const x = box.x + margin; const y = box.y + margin; page.setCropBox(x, y, w, h); page.setMediaBox(x, y, w, h); page.setBleedBox(x, y, w, h); page.setTrimBox(x, y, w, h); page.setArtBox(x, y, w, h);
Margin is clamped at zero. Width/height are at least 1pt so you cannot invert the box. All five page boxes are set to the same rectangle because PDF consumers pick different boxes for display vs print. Bytes are wrapped in a Blob and downloaded as cropped.pdf.
The Local Storage Sandbox & Volatile Data Pipelines
The source File stays in React state. The output Blob is revoked after click. There is no IndexedDB of cropped drafts. GC collects pdf-lib documents when the async function returns.
Verifying True Privacy: How to Audit Your Browser's Network Tab
F12, Network, clear. Drop a dummy PDF, crop, download. You may see /pdf.worker.min.mjs from this origin—that is the local worker script, not your file. Confirm no POST whose size matches the PDF.