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

@ -63,7 +63,10 @@ registerEndpoint("/split-pdf", "_split", upload.single("file"), Operations.split
pageIndexes: Joi.string().required(),
}).required());
//organise/arrange
registerEndpoint("/rearrange-pages", "", upload.single("file"), Operations.arrangePages, Joi.object({
file: PdfFileSchema.required(),
arrangementConfig: Joi.string().required(),
}).required());
registerEndpoint("/rotate-pdf", "", upload.single("file"), Operations.rotatePages, Joi.object({
file: PdfFileSchema.required(),

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;
}

View File

@ -1,4 +1,5 @@
import { arrangePages, ArrangePagesParamsType } from './functions/arrangePages'
import { extractPages, ExtractPagesParamsType } from "./functions/extractPages";
import { impose, ImposeParamsBaseType, ImposeParamsType } from "./functions/impose";
import { mergePDFs, MergeParamsType } from './functions/mergePDFs';
@ -6,7 +7,6 @@ import { removeBlankPages, RemoveBlankPagesParamsType } from "./functions/remove
import { rotatePages, RotateParamsType } from './functions/rotatePages';
import { scaleContent, ScaleContentParamsType} from './functions/scaleContent';
import { scalePage, ScalePageParamsType } from './functions/scalePage';
import { sortPagesWithPreset, SortPagesWithPresetParamsType } from './functions/sortPagesWithPreset'
import { splitPagesByPreset, SplitPageByPresetParamsType } from './functions/splitPagesByPreset';
import { splitPdfByIndex, SplitPdfByIndexParamsType } from './functions/splitPdfByIndex';
import { updateMetadata, UpdateMetadataParams } from "./functions/updateMetadata";
@ -17,6 +17,7 @@ import { Override } from '../declarations/TypeScriptUtils'
// Import injected libraries here!
const toExport = {
arrangePages,
extractPages,
impose,
mergePDFs,
@ -24,7 +25,6 @@ const toExport = {
rotatePages,
scaleContent,
scalePage,
sortPagesWithPreset,
splitPagesByPreset,
splitPdfByIndex,
updateMetadata,
@ -32,6 +32,7 @@ const toExport = {
export default toExport;
export type OperationsParametersBaseType = {
arrangePages: ArrangePagesParamsType
extractPages: ExtractPagesParamsType;
impose: ImposeParamsBaseType;
mergePDFs: MergeParamsType;
@ -39,7 +40,6 @@ export type OperationsParametersBaseType = {
rotatePages: RotateParamsType;
scaleContent: ScaleContentParamsType;
scalePage: ScalePageParamsType;
sortPagesWithPreset: SortPagesWithPresetParamsType;
splitPagesByPreset: SplitPageByPresetParamsType;
splitPdfByIndex: SplitPdfByIndexParamsType;
updateMetadata: UpdateMetadataParams;

View File

@ -84,7 +84,7 @@ export async function * traverseOperations(operations: Action[], input: PdfFile[
break;
case "sortPagesWithPreset":
yield* nToN(input, action, async (input) => {
const newPdf = await Operations.sortPagesWithPreset({file: input, sortPreset: action.values["sortPreset"]});
const newPdf = await Operations.arrangePages({file: input, arrangementConfig: action.values["arrangementConfig"]});
return newPdf;
});
break;