Stirling-PDF/shared-operations/functions/splitPDF.js

27 lines
799 B
JavaScript
Raw Normal View History

2023-10-26 20:53:02 +02:00
2023-10-26 22:41:48 +02:00
import { PDFDocument } from 'pdf-lib';
2023-10-26 20:53:02 +02:00
import { createSubDocument } from "./shared/extractPages.js";
2023-10-17 03:40:54 +02:00
2023-10-26 20:53:02 +02:00
export async function splitPDF(snapshot, splitAfterPageArray) {
2023-10-26 22:41:48 +02:00
const pdfDoc = await 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) {
2023-10-26 20:53:02 +02:00
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
2023-10-17 03:40:54 +02:00
splitAfter = splitAfterPageArray.shift();
pagesArray = [];
}
pagesArray.push(i);
2023-10-17 03:40:54 +02:00
}
2023-10-26 20:53:02 +02:00
subDocuments.push(await createSubDocument(pdfDoc, pagesArray));
2023-10-17 03:40:54 +02:00
pagesArray = [];
return subDocuments;
};