Job and bank forms often ask for all your documents in one PDF. You can merge PDF files directly in the browser with the open source library pdf-lib, so the files never have to be uploaded to a server. This post shows the core idea.
Install
npm install pdf-lib
Merge several PDFs
import { PDFDocument } from "pdf-lib";
async function mergePdfs(files) {
const merged = await PDFDocument.create();
for (const file of files) {
const bytes = await file.arrayBuffer();
const source = await PDFDocument.load(bytes);
const pages = await merged.copyPages(source, source.getPageIndices());
pages.forEach((page) => merged.addPage(page));
}
return await merged.save(); // Uint8Array
}
The order of the files array decides the order of the pages, so sort it before you call the function.
Download the result
function download(bytes, name) {
const blob = new Blob([bytes], { type: "application/pdf" });
const url = URL.createObjectURL(blob);
const a = document.createElement("a");
a.href = url;
a.download = name;
a.click();
URL.revokeObjectURL(url);
}
Things to watch
- Encrypted PDFs: a file protected by a password cannot be loaded as is. Ask the user to unlock it first.
- Memory: every file is read fully into memory, so a merge of very large files can be slow on a phone. Show a progress message.
- File size: merging does not shrink anything. If a form has a size limit, compress the scans before you merge them, for example by scanning at a lower DPI.
- Order: let people reorder files before merging. A wrong page order is the most common complaint.
- Form fields: copying pages can leave interactive form fields or document-level structures behind. If the fields matter, test the merged file with a filled form, open it in a second viewer, and flatten a copy first if needed.
- Large inputs: show the total input size before merging and stop with a clear message before the browser becomes unresponsive, especially on phones.
Update: the form-field and large-input notes above came from reader comments. Thanks for the feedback.
Disclosure: I work on PixelKaro, a set of free browser-based image and PDF tools (https://pixelkaro.com) that process files on your device. The code above is a simplified example of the general approach.
Top comments (4)
Client-side merging is a strong privacy default, but large documents make memory behavior part of the UX contract. Showing an estimated input size, failing gracefully before the browser becomes unresponsive, and preserving file order explicitly are the details that make a no-server workflow trustworthy in practice.
Agreed, memory is the real limit on phones. Showing the total input size up front and stopping before the browser hangs is a good UX rule. Thanks for the detail, I'll add a note about it to the post.
One caveat for forms: copying pages can leave interactive fields or document-level structures behind. Test the merged file with a filled form and reopen it in a second viewer; if the fields matter, flatten a copy first or use a merger that preserves form structures.
Fair point. copyPages doesn't reliably carry over form fields, so for forms it's worth testing the merged file in a second viewer, or flattening a copy first. I'll add this caveat to the post. Thanks for the feedback.