mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2024-12-21 19:08:24 +01:00
97e4eab7bb
Split express into different ports for easier migration Typed page ops that had warnings
28 lines
913 B
TypeScript
28 lines
913 B
TypeScript
|
|
import { PDFDocument } from 'pdf-lib';
|
|
|
|
import { createSubDocument } from "./common/createSubDocument.js";
|
|
|
|
export async function splitPDF(snapshot: string | Uint8Array | ArrayBuffer, splitAfterPageArray: number[]): Promise<Uint8Array[]> {
|
|
|
|
const pdfDoc = await PDFDocument.load(snapshot)
|
|
|
|
const numberOfPages = pdfDoc.getPages().length;
|
|
|
|
let pagesArray: number[] = [];
|
|
let splitAfter = splitAfterPageArray.shift();
|
|
const subDocuments: Uint8Array[] = [];
|
|
|
|
for (let i = 0; i < numberOfPages; i++) {
|
|
if(splitAfter && i > splitAfter && pagesArray.length > 0) {
|
|
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
|
|
splitAfter = splitAfterPageArray.shift();
|
|
pagesArray = [];
|
|
}
|
|
pagesArray.push(i);
|
|
}
|
|
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
|
|
pagesArray = [];
|
|
|
|
return subDocuments;
|
|
}; |