Stirling-PDF/public/functions/splitPDF.js

24 lines
802 B
JavaScript
Raw Normal View History

import { createSubDocument } from "./shared/createSubDocument.js";
2023-10-17 03:40:54 +02:00
export async function splitPDF(snapshot, splitAfterPageArray, PDFLib) {
const pdfDoc = await PDFLib.PDFDocument.load(snapshot)
2023-10-17 03:40:54 +02:00
const numberOfPages = pdfDoc.getPages().length;
let pagesArray = [];
let splitAfter = splitAfterPageArray.shift();
const subDocuments = [];
for (let i = 0; i < numberOfPages; i++) {
if(i > splitAfter && pagesArray.length > 0) {
subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib));
2023-10-17 03:40:54 +02:00
splitAfter = splitAfterPageArray.shift();
pagesArray = [];
}
pagesArray.push(i);
}
subDocuments.push(await createSubDocument(pdfDoc, pagesArray, PDFLib));
2023-10-17 03:40:54 +02:00
pagesArray = [];
return subDocuments;
};