extract working with new standard and dynamically

This commit is contained in:
Felix Kaspar 2024-05-10 23:01:18 +02:00
parent 6eed4a3238
commit 113f87aa3e
4 changed files with 74 additions and 5 deletions

View File

@ -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."
}
}
}

View File

@ -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<PdfF
newFile.filename += "_extractedPages";
return newFile;
}
export class ExtractPages extends Operator {
static type = "extractPages";
/**
* Validation & Localisation
*/
protected static inputSchema = JoiPDFFileSchema.label(i18next.t("inputs.pdffile.name")).description(i18next.t("inputs.pdffile.description"));
protected static valueSchema = Joi.object({
pageIndexes: Joi.array().items(Joi.number().integer()).required()
.label(i18next.t("values.pageIndexes.friendlyName", { ns: "extractPages" })).description(i18next.t("values.pageIndexes.description", { ns: "extractPages" }))
.example("3").example("4").required()
});
protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description"));
static schema = Joi.object({
input: ExtractPages.inputSchema,
values: ExtractPages.valueSchema.required(),
output: ExtractPages.outputSchema
}).label(i18next.t("friendlyName", { ns: "extractPages" })).description(i18next.t("description", { ns: "extractPages" }));
/**
* Logic
*/
/** PDF-Imposition, PDF-N-Up: Put multiple pages of the input document into a single page of the output document. - see: {@link https://en.wikipedia.org/wiki/N-up} */
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
return oneToOne<PdfFile, PdfFile>(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;
});
}
}

View File

@ -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";

View File

@ -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<typeof Operator | undefined> {
// Check if exists
@ -9,7 +9,11 @@ export async function getOperatorByName(name: string): Promise<typeof Operator |
i18next.loadNamespaces(name, (err, t) => { 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[] {