2023-11-17 15:52:44 +03:00
|
|
|
|
|
|
|
import { PdfFile } from '../wrappers/PdfFile.js';
|
|
|
|
import { parsePageIndexSpecification } from './common/pageIndexesUtils'
|
|
|
|
import { splitPagesByIndex } from './common/splitPagesByIndex.js';
|
|
|
|
|
2023-11-17 16:11:06 +03:00
|
|
|
export type SplitPdfByIndexParamsType = {
|
2023-11-17 15:52:44 +03:00
|
|
|
file: PdfFile;
|
|
|
|
pageIndexes: string | number[];
|
|
|
|
}
|
2023-11-17 16:11:06 +03:00
|
|
|
export async function splitPdfByIndex(params: SplitPdfByIndexParamsType): Promise<PdfFile[]> {
|
2023-11-17 15:52:44 +03:00
|
|
|
const { file, pageIndexes } = params;
|
|
|
|
const pdfLibDocument = await file.pdfLibDocument;
|
|
|
|
|
|
|
|
var indexes = pageIndexes;
|
|
|
|
|
|
|
|
if (!Array.isArray(indexes)) {
|
|
|
|
indexes = parsePageIndexSpecification(indexes, pdfLibDocument.getPageCount());
|
|
|
|
}
|
|
|
|
|
|
|
|
const newFiles = await splitPagesByIndex(file, indexes);
|
|
|
|
for (let i = 0; i < newFiles.length; i++) {
|
|
|
|
newFiles[i].filename += "_split-"+i;
|
|
|
|
}
|
|
|
|
return newFiles;
|
|
|
|
}
|