diff --git a/shared-operations/public/locales/extractPages/en.json b/shared-operations/public/locales/extractPages/en.json new file mode 100644 index 000000000..630ebc204 --- /dev/null +++ b/shared-operations/public/locales/extractPages/en.json @@ -0,0 +1,10 @@ +{ + "friendlyName": "Extract", + "description": "Extract one ore more pages from a PDF-File", + "values": { + "pageIndexes": { + "friendlyName": "Page Indexes", + "description": "An array of indexes of pages you want to extract." + } + } +} \ No newline at end of file diff --git a/shared-operations/src/functions/extractPages.ts b/shared-operations/src/functions/extractPages.ts index 6252ba4af..a16e5bcae 100644 --- a/shared-operations/src/functions/extractPages.ts +++ b/shared-operations/src/functions/extractPages.ts @@ -1,5 +1,12 @@ -import { PdfFile } from "../wrappers/PdfFile"; +import { PdfFile, RepresentationType } from "../wrappers/PdfFile"; +import { Operator, Progress, oneToOne } from "."; + +import Joi from "@stirling-tools/joi"; +import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi"; + +import i18next from "i18next"; + import { getPages } from "./common/getPagesByIndex"; import { parsePageIndexSpecification } from "./common/pageIndexesUtils"; @@ -21,3 +28,50 @@ export async function extractPages(params: ExtractPagesParamsType): Promise void): Promise { + return oneToOne(input, async (input, index, max) => { + const pdfLibDocument = await input.pdfLibDocument; + + let indexes = this.actionValues.pageIndexes; + + if (!Array.isArray(indexes)) { + indexes = parsePageIndexSpecification(indexes, pdfLibDocument.getPageCount()); + } + + const newFile = await getPages(input, indexes); + newFile.filename += "_extractedPages"; + progressCallback({ curFileProgress: 1, operationProgress: index/max }); + + return newFile; + }); + } +} diff --git a/shared-operations/src/functions/impose.ts b/shared-operations/src/functions/impose.ts index dbd1bb57b..8b715dafb 100644 --- a/shared-operations/src/functions/impose.ts +++ b/shared-operations/src/functions/impose.ts @@ -2,13 +2,14 @@ import { PdfFile, RepresentationType } from "../wrappers/PdfFile"; import { Operator, Progress, oneToOne } from "."; -import * as pdfcpuWrapper from "#pdfcpu"; // This is updated by tsconfig.json/paths for the context (browser, node, etc.) this module is used in. - import Joi from "@stirling-tools/joi"; import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi"; import i18next from "i18next"; +import * as pdfcpuWrapper from "#pdfcpu"; // This is updated by tsconfig.json/paths for the context (browser, node, etc.) this module is used in. + + export class Impose extends Operator { static type = "impose"; diff --git a/shared-operations/src/workflow/operatorAccessor.ts b/shared-operations/src/workflow/operatorAccessor.ts index deca03279..e2c5f792d 100644 --- a/shared-operations/src/workflow/operatorAccessor.ts +++ b/shared-operations/src/workflow/operatorAccessor.ts @@ -1,7 +1,7 @@ import { Operator } from "../functions"; import i18next from "i18next"; -const compileTimeOperatorList: string[] = ["impose"]; import.meta.compileTime("./listOperatorsInDir.ts"); // The will compile to ["impose", "extractPages", etc...] +const compileTimeOperatorList: string[] = import.meta.compileTime("./listOperatorsInDir.ts"); // The will compile to ["impose", "extractPages", etc...] export async function getOperatorByName(name: string): Promise { // Check if exists @@ -9,7 +9,11 @@ export async function getOperatorByName(name: string): Promise { if (err) throw err; }); const loadedModule = await import("../functions/" + name + ".ts"); - return loadedModule[capitalizeFirstLetter(name)]; + const operator = loadedModule[capitalizeFirstLetter(name)]; + if(!operator) { + throw Error("This operator does not export its class in the correct format.") + } + return operator; } export function listOperatorNames(): string[] {