added rearrange-pages endpoint
This commit is contained in:
27
shared-operations/src/functions/arrangePages.ts
Normal file
27
shared-operations/src/functions/arrangePages.ts
Normal 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;
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user