added rearrange-pages endpoint

This commit is contained in:
Saud Fatayerji
2023-11-17 17:13:42 +03:00
parent 26cb467156
commit 3496a5d658
5 changed files with 35 additions and 30 deletions

View File

@@ -0,0 +1,27 @@
import { PdfFile } from '../wrappers/PdfFile.js';
import { Sorts } from './common/pageIndexesSorting.js';
import { getPages } from './common/getPagesByIndex.js';
import { parsePageIndexSpecification } from './common/pageIndexesUtils.js';
export type ArrangePagesParamsType = {
file: PdfFile;
arrangementConfig: string; // a member of Sorts, or a page index specification
}
export async function arrangePages(params: ArrangePagesParamsType) {
const { file, arrangementConfig } = params;
const pdfLibDocument = await file.pdfLibDocument;
const pageCount = pdfLibDocument.getPageCount();
let sortIndexes: number[];
if (arrangementConfig in Sorts) {
const sortFunction = Sorts[arrangementConfig];
sortIndexes = sortFunction(pageCount);
} else {
sortIndexes = parsePageIndexSpecification(arrangementConfig, pageCount);
}
const newFile = await getPages(file, sortIndexes);
newFile.filename += "arrangedPages"
return newFile;
}

View File

@@ -1,25 +0,0 @@
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;
}