scalePage

This commit is contained in:
Felix Kaspar 2024-05-18 20:10:57 +02:00
parent 3eca56f8c6
commit d0dbb7e708
3 changed files with 71 additions and 83 deletions

View File

@ -22,7 +22,8 @@ export class RotatePages extends Operator {
rotation: Joi.alternatives().try( rotation: Joi.alternatives().try(
Joi.number().integer().min(-360).max(360), Joi.number().integer().min(-360).max(360),
CommaArrayJoiExt.comma_array().items(Joi.number().integer().min(-360).max(360)) CommaArrayJoiExt.comma_array().items(Joi.number().integer().min(-360).max(360))
).label(i18next.t("values.rotation.friendlyName", { ns: "rotatePages" })).description(i18next.t("values.rotation.description", { ns: "rotatePages" })) ).required()
.label(i18next.t("values.rotation.friendlyName", { ns: "rotatePages" })).description(i18next.t("values.rotation.description", { ns: "rotatePages" }))
.example("90").example("-180").example("[90, 0, 270]"), .example("90").example("-180").example("[90, 0, 270]"),
}); });
protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description")); protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description"));

View File

@ -22,7 +22,8 @@ export class ScaleContent extends Operator {
scaleFactor: Joi.alternatives().try( scaleFactor: Joi.alternatives().try(
Joi.number(), Joi.number(),
CommaArrayJoiExt.comma_array().items(Joi.number()) CommaArrayJoiExt.comma_array().items(Joi.number())
).label(i18next.t("values.scaleFactor.friendlyName", { ns: "scaleContent" })).description(i18next.t("values.scaleFactor.description", { ns: "scaleContent" })) ).required()
.label(i18next.t("values.scaleFactor.friendlyName", { ns: "scaleContent" })).description(i18next.t("values.scaleFactor.description", { ns: "scaleContent" }))
.example("2").example("1.5").example("[1, 1.5, 0.9]"), .example("2").example("1.5").example("[1, 1.5, 0.9]"),
}); });
protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description")); protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description"));

View File

@ -1,91 +1,77 @@
import { Operator, Progress, oneToOne } from ".";
import Joi from "@stirling-tools/joi"; import Joi from "@stirling-tools/joi";
import { PDFPage } from "pdf-lib";
import { PdfFile, RepresentationType } from "../wrappers/PdfFile";
import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi"; import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi";
import i18next from "i18next";
const whSchema = Joi.string().custom((value, helpers) => { import { PDFPage } from "pdf-lib";
console.log("value.pageSize", typeof value); import { PdfFile, RepresentationType } from "../wrappers/PdfFile";
try {
const obj = JSON.parse(value); export class ScalePage extends Operator {
if (!obj.width && !obj.height) { static type = "scalePage";
return helpers.error("any.required", { message: "At least one of width/height must be present" });
} /**
if (typeof obj.width != "number" && typeof obj.width != "undefined") { * Validation & Localisation
return helpers.error("any.invalid", { message: "Width must be a number if present" }); */
}
if (typeof obj.height != "number" && typeof obj.height != "undefined") { protected static inputSchema = JoiPDFFileSchema.label(i18next.t("inputs.pdffile.name")).description(i18next.t("inputs.pdffile.description"));
return helpers.error("any.invalid", { message: "Height must be a number if present" }); protected static valueSchema = Joi.object({
} height: Joi.number().min(0)
return obj; .label(i18next.t("values.height.friendlyName", { ns: "scalePage" })).description(i18next.t("values.height.description", { ns: "scalePage" }))
} catch (error) { .example("842").example("595").example("1190"),
return helpers.error("any.invalid", { message: "Value must be a valid JSON" }); width: Joi.number().min(0)
.label(i18next.t("values.width.friendlyName", { ns: "scalePage" })).description(i18next.t("values.width.description", { ns: "scalePage" }))
.example("595").example("420").example("842"),
}).or("height", "width");
protected static outputSchema = JoiPDFFileSchema.label(i18next.t("outputs.pdffile.name")).description(i18next.t("outputs.pdffile.description"));
static schema = Joi.object({
input: ScalePage.inputSchema,
values: ScalePage.valueSchema.required(),
output: ScalePage.outputSchema
}).label(i18next.t("friendlyName", { ns: "scalePage" })).description(i18next.t("description", { ns: "scalePage" }));
/**
* Logic
*/
/** Detect and remove white pages */
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
return oneToOne<PdfFile, PdfFile>(input, async (input, index, max) => {
const pdfDoc = await input.pdfLibDocument;
const pages = pdfDoc.getPages();
pages.forEach(page => { ScalePage.resize(page, { height: this.actionValues.height, width: this.actionValues.width }) });
progressCallback({ curFileProgress: 1, operationProgress: index/max });
return new PdfFile(input.originalFilename, pdfDoc, RepresentationType.PDFLibDocument, input.filename+"_scaledPages");
});
} }
});
export const ScalePageSchema = Joi.object({ static resize(page: PDFPage, newSize: {width?:number,height?:number}) {
file: JoiPDFFileSchema.required(), const calculatedSize = ScalePage.calculateSize(page, newSize);
pageSize: Joi.alternatives().try(whSchema, Joi.array().items(whSchema)).required(), const xRatio = calculatedSize.width / page.getWidth();
}); const yRatio = calculatedSize.height / page.getHeight();
page.setSize(calculatedSize.width, calculatedSize.height);
export interface ScalePageParamsType { page.scaleContent(xRatio, yRatio);
file: PdfFile;
pageSize: { width?:number,height?:number }|{ width?:number,height?:number }[];
}
export async function scalePage(params: ScalePageParamsType): Promise<PdfFile> {
const { file, pageSize } = params;
const pdfDoc = await file.pdfLibDocument;
const pages = pdfDoc.getPages();
if (Array.isArray(pageSize)) {
if (pageSize.length != pages.length) {
throw new Error(`Number of given sizes '${pageSize.length}' is not the same as the number of pages '${pages.length}'`);
}
for (let i=0; i<pageSize.length; i++) {
resize(pages[i], pageSize[i]);
}
} else {
pages.forEach(page => { resize(page, pageSize) });
} }
return new PdfFile(file.originalFilename, pdfDoc, RepresentationType.PDFLibDocument, file.filename+"_scaledPages"); static calculateSize(page: PDFPage, newSize: {width?:number,height?:number}): {width:number,height:number} {
} if (!newSize.width && !newSize.height){
throw new Error(`Sizes '${newSize}' cannot have null width and null height`);
function resize(page: PDFPage, newSize: {width?:number,height?:number}) { } else if (!newSize.width && newSize.height) {
const calculatedSize = calculateSize(page, newSize); const oldSize = page.getSize();
const xRatio = calculatedSize.width / page.getWidth(); const ratio = oldSize.width / oldSize.height;
const yRatio = calculatedSize.height / page.getHeight(); return { width: newSize.height * ratio, height: newSize.height };
} else if (newSize.width && !newSize.height) {
page.setSize(calculatedSize.width, calculatedSize.height); const oldSize = page.getSize();
page.scaleContent(xRatio, yRatio); const ratio = oldSize.height / oldSize.width;
} return { width: newSize.width, height: newSize.width * ratio };
}
function calculateSize(page: PDFPage, newSize: {width?:number,height?:number}): {width:number,height:number} { return { width: newSize.width, height: newSize.height };
if (!newSize.width && !newSize.height){
throw new Error(`Sizes '${newSize}' cannot have null width and null height`);
} else if (!newSize.width && newSize.height) {
const oldSize = page.getSize();
const ratio = oldSize.width / oldSize.height;
return { width: newSize.height * ratio, height: newSize.height };
} else if (newSize.width && !newSize.height) {
const oldSize = page.getSize();
const ratio = oldSize.height / oldSize.width;
return { width: newSize.width, height: newSize.width * ratio };
} }
return { width: newSize.width, height: newSize.height }; }
}
export const PageSize = Object.freeze({
a4: {
width: 594.96,
height: 841.92
},
letter: {
width: 612,
height: 792
}
});