Split PDF Utility
The splitPDF function allows you to slice an existing PDF document into multiple files based on page range instructions.
API Signature
function splitPDF(
file: File | Blob | ArrayBuffer | Uint8Array,
pageRanges: string
): Promise<Uint8Array[]>
Parameters
file: The input PDF file or buffer.pageRanges: A comma-separated string containing ranges (e.g."1-3, 5, 7-10"). Note that page numbers are 1-indexed.
Code Example
import { splitPDF } from "@manojcoder/pdf-utils";
async function performSplit(file: File) {
// Define target splits
const ranges = "1-2, 4-5"; // Slices pages 1 to 2, and pages 4 to 5
try {
const documents: Uint8Array[] = await splitPDF(file, ranges);
// Process the split documents
documents.forEach((bytes, index) => {
const blob = new Blob([bytes], { type: "application/pdf" });
const url = URL.createObjectURL(blob);
console.log(`Document segment ${index + 1} ready at: ${url}`);
});
} catch (error) {
console.error("Splitting failed:", error);
}
}
Bounds Check: If you specify a page number that exceeds the total pages of the document, splitPDF will throw a validation error.