Merge PDF Utility
The mergePDFs function combines an array of PDF files, blobs, or buffers sequentially into a single consolidated PDF document.
API Signature
function mergePDFs(
files: (File | Blob | ArrayBuffer | Uint8Array)[]
): Promise<Uint8Array>
Parameters
files: An array of input PDF documents to concatenate.
Code Example
Here is how you can merge multiple files selected by a user in React, Vue, or vanilla Javascript:
import { mergePDFs } from "@manojcoder/pdf-utils";
async function combineFiles(filesList: File[]) {
try {
// Merge the array of files client-side
const mergedBytes = await mergePDFs(filesList);
// Convert output Uint8Array to a Blob
const mergedBlob = new Blob([mergedBytes], { type: "application/pdf" });
const downloadUrl = URL.createObjectURL(mergedBlob);
// Trigger download
const link = document.createElement("a");
link.href = downloadUrl;
link.download = "combined_document.pdf";
link.click();
} catch (error) {
console.error("Merging failed:", error);
}
}
Memory Optimization: The merging process is optimized using stream copies. Ensure files are cleared from memory when no longer needed by calling URL.revokeObjectURL(url).