Stirling-PDF/shared-operations/src/functions/sortPagesWithPreset.ts
2023-11-17 00:45:37 +03:00

26 lines
804 B
TypeScript

import { PdfFile } from '../wrappers/PdfFile.js';
import { sorts } from './common/pageIndexesSorting.js';
import { getPages } from './common/getPagesByIndex.js';
export type SortPagesWithPresetParamsType = {
file: PdfFile;
sortPreset: string;
}
export async function sortPagesWithPreset(params: SortPagesWithPresetParamsType) {
const { file, sortPreset } = params;
const pdfLibDocument = await file.pdfLibDocument;
if (!(sortPreset in sorts)) {
throw new Error("Supplied parameters not supported");
}
const sortFunction = sorts[sortPreset];
const pageCount = pdfLibDocument.getPageCount();
const sortIndexes = sortFunction(pageCount);
const newFile = await getPages(file, sortIndexes);
newFile.filename += "_sortedPages"
return newFile;
}