How I built offline PDF processing with pdf-lib and pdf.js in the browser
Every "free online PDF tool" does the same thing: you drag a file into the page, and that file is uploaded to a server you'll never see, in a region you can't name, for a retention period nobody specifies. I wanted a PDF toolbox that never uploads anything. Not "we delete it after an hour" — never sends it at all. That meant all parsing, modification and serialization had to happen inside the…
Building offline PDF processing using pdf-lib and pdf.js in the browser presents several challenges due to the constraints of running everything within a single tab and the limited memory available on mobile devices. The primary libraries used are pdf-lib for writing and pdf.js for reading. pdf-lib enables merging, splitting, rotating, deleting pages, watermarking, and encrypting PDFs, while pdf.js handles rendering thumbnails and page previews, allowing users to select pages before making any modifications.
To avoid any network requests and CORS issues, pdf.js is built from a blob, inlining the worker bundle at build time, which costs about 1MB of HTML but eliminates all network requests and CORS concerns.
Merging PDFs is straightforward using pdf-lib's copy-pages method, which simply copies pages from the input files into a new document. Splitting and rotating PDFs follow a similar approach. However, the real challenges arise in compression and encryption. pdf-lib has limitations in re-encoding embedded images, which means that compressing a PDF in the browser typically only reduces redundancy in the streams it can touch, leaving heavily scanned PDFs with minimal size reduction.
The implementation to achieve more useful compression involves re-deflating streams manually using pako at a chosen level. The implementation comes with two caveats: Low compression can sometimes result in a larger file, and image-heavy PDFs will barely change in size as JPEG compression doesn't help much.
Encryption in PDFs is another major hurdle, as pdf-lib does not implement the PDF security handler standard. Encrypting and decrypting PDFs requires a separate layer that uses the Web Crypto API for AES-256 or RC4-128 encryption and decryption. There are several complexities to consider in this process, including object streams that pdf-lib decompresses at load time, key length derivation from the /Length field, and handling empty user passwords, which are common in some PDFs.
Memory usage also poses a significant challenge, as mobile browsers are where this design is tested. Every arrayBuffer() held in memory is a full copy of the file, and the PDFDocument.load() method keeps the entire object graph in memory. Therefore, releasing result blobs and optimizing memory usage is crucial to prevent issues on mobile devices.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.
This story
This is one outlet's version. Read the fullest account.