mirror of
https://github.com/Frooodle/Stirling-PDF.git
synced 2025-01-10 00:06:51 +01:00
34 lines
1.1 KiB
TypeScript
34 lines
1.1 KiB
TypeScript
|
|
import { selectPages } from "./subDocumentFunctions";
|
|
import { PdfFile } from '../wrappers/PdfFile';
|
|
|
|
export type SplitPdfParamsType = {
|
|
file: PdfFile;
|
|
splitAfterPageArray: number[];
|
|
}
|
|
|
|
export async function splitPDF(params: SplitPdfParamsType): Promise<PdfFile[]> {
|
|
const { file, splitAfterPageArray } = params;
|
|
|
|
const byteFile = await file.convertToPdfLibFile();
|
|
if (!byteFile?.pdfLib) return [];
|
|
|
|
const numberOfPages = byteFile.pdfLib.getPages().length;
|
|
|
|
let pagesArray: number[] = [];
|
|
let splitAfter = splitAfterPageArray.shift();
|
|
const subDocuments: PdfFile[] = [];
|
|
|
|
for (let i = 0; i < numberOfPages; i++) {
|
|
if(splitAfter && i > splitAfter && pagesArray.length > 0) {
|
|
subDocuments.push(await selectPages({file:byteFile, pagesToExtractArray:pagesArray}));
|
|
splitAfter = splitAfterPageArray.shift();
|
|
pagesArray = [];
|
|
}
|
|
pagesArray.push(i);
|
|
}
|
|
subDocuments.push(await selectPages({file:byteFile, pagesToExtractArray:pagesArray}));
|
|
pagesArray = [];
|
|
|
|
return subDocuments;
|
|
}; |