Stirling-PDF/functions/splitPDF.js
2023-10-20 02:10:03 +02:00

24 lines
812 B
JavaScript

import { PDFDocument, ParseSpeeds } from 'pdf-lib'
import { createSubDocument } from "./extractPages.js";
export const splitPDF = async (snapshot, splitAfterPageArray) => {
const pdfDoc = await PDFDocument.load(snapshot)
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));
splitAfter = splitAfterPageArray.shift();
pagesArray = [];
}
pagesArray.push(i);
}
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
pagesArray = [];
return subDocuments;
};