2023-10-26 20:53:02 +02:00
|
|
|
|
2023-11-12 02:46:09 +01:00
|
|
|
import { selectPages } from "./subDocumentFunctions";
|
|
|
|
import { PdfFile } from '../wrappers/PdfFile';
|
2023-11-08 01:33:22 +01:00
|
|
|
|
2023-11-14 01:26:42 +01:00
|
|
|
export type SplitPdfParamsType = {
|
|
|
|
file: PdfFile;
|
|
|
|
splitAfterPageArray: number[];
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function splitPDF(params: SplitPdfParamsType): Promise<PdfFile[]> {
|
|
|
|
const { file, splitAfterPageArray } = params;
|
|
|
|
|
2023-11-12 02:46:09 +01:00
|
|
|
const byteFile = await file.convertToPdfLibFile();
|
|
|
|
if (!byteFile?.pdfLib) return [];
|
2023-10-17 03:40:54 +02:00
|
|
|
|
2023-11-12 02:46:09 +01:00
|
|
|
const numberOfPages = byteFile.pdfLib.getPages().length;
|
2023-10-17 03:40:54 +02:00
|
|
|
|
2023-11-08 02:24:16 +01:00
|
|
|
let pagesArray: number[] = [];
|
2023-10-17 03:40:54 +02:00
|
|
|
let splitAfter = splitAfterPageArray.shift();
|
2023-11-12 02:46:09 +01:00
|
|
|
const subDocuments: PdfFile[] = [];
|
2023-10-17 03:40:54 +02:00
|
|
|
|
|
|
|
for (let i = 0; i < numberOfPages; i++) {
|
2023-11-08 01:33:22 +01:00
|
|
|
if(splitAfter && i > splitAfter && pagesArray.length > 0) {
|
2023-11-14 01:26:42 +01:00
|
|
|
subDocuments.push(await selectPages({file:byteFile, pagesToExtractArray:pagesArray}));
|
2023-10-17 03:40:54 +02:00
|
|
|
splitAfter = splitAfterPageArray.shift();
|
|
|
|
pagesArray = [];
|
|
|
|
}
|
2023-11-08 00:11:49 +01:00
|
|
|
pagesArray.push(i);
|
2023-10-17 03:40:54 +02:00
|
|
|
}
|
2023-11-14 01:26:42 +01:00
|
|
|
subDocuments.push(await selectPages({file:byteFile, pagesToExtractArray:pagesArray}));
|
2023-10-17 03:40:54 +02:00
|
|
|
pagesArray = [];
|
|
|
|
|
|
|
|
return subDocuments;
|
|
|
|
};
|