mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-22 23:45:02 +00:00
scalePage
This commit is contained in:
parent
3eca56f8c6
commit
d0dbb7e708
@ -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"));
|
||||||
|
@ -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"));
|
||||||
|
@ -1,62 +1,58 @@
|
|||||||
|
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);
|
|
||||||
if (!obj.width && !obj.height) {
|
|
||||||
return helpers.error("any.required", { message: "At least one of width/height must be present" });
|
|
||||||
}
|
|
||||||
if (typeof obj.width != "number" && typeof obj.width != "undefined") {
|
|
||||||
return helpers.error("any.invalid", { message: "Width must be a number if present" });
|
|
||||||
}
|
|
||||||
if (typeof obj.height != "number" && typeof obj.height != "undefined") {
|
|
||||||
return helpers.error("any.invalid", { message: "Height must be a number if present" });
|
|
||||||
}
|
|
||||||
return obj;
|
|
||||||
} catch (error) {
|
|
||||||
return helpers.error("any.invalid", { message: "Value must be a valid JSON" });
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
export const ScalePageSchema = Joi.object({
|
export class ScalePage extends Operator {
|
||||||
file: JoiPDFFileSchema.required(),
|
static type = "scalePage";
|
||||||
pageSize: Joi.alternatives().try(whSchema, Joi.array().items(whSchema)).required(),
|
|
||||||
});
|
/**
|
||||||
|
* Validation & Localisation
|
||||||
|
*/
|
||||||
|
|
||||||
|
protected static inputSchema = JoiPDFFileSchema.label(i18next.t("inputs.pdffile.name")).description(i18next.t("inputs.pdffile.description"));
|
||||||
|
protected static valueSchema = Joi.object({
|
||||||
|
height: Joi.number().min(0)
|
||||||
|
.label(i18next.t("values.height.friendlyName", { ns: "scalePage" })).description(i18next.t("values.height.description", { ns: "scalePage" }))
|
||||||
|
.example("842").example("595").example("1190"),
|
||||||
|
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" }));
|
||||||
|
|
||||||
|
|
||||||
export interface ScalePageParamsType {
|
/**
|
||||||
file: PdfFile;
|
* Logic
|
||||||
pageSize: { width?:number,height?:number }|{ width?:number,height?:number }[];
|
*/
|
||||||
}
|
|
||||||
|
|
||||||
export async function scalePage(params: ScalePageParamsType): Promise<PdfFile> {
|
/** Detect and remove white pages */
|
||||||
const { file, pageSize } = params;
|
async run(input: PdfFile[], progressCallback: (state: Progress) => void): Promise<PdfFile[]> {
|
||||||
|
return oneToOne<PdfFile, PdfFile>(input, async (input, index, max) => {
|
||||||
const pdfDoc = await file.pdfLibDocument;
|
const pdfDoc = await input.pdfLibDocument;
|
||||||
const pages = pdfDoc.getPages();
|
const pages = pdfDoc.getPages();
|
||||||
|
|
||||||
if (Array.isArray(pageSize)) {
|
pages.forEach(page => { ScalePage.resize(page, { height: this.actionValues.height, width: this.actionValues.width }) });
|
||||||
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}'`);
|
progressCallback({ curFileProgress: 1, operationProgress: index/max });
|
||||||
}
|
|
||||||
for (let i=0; i<pageSize.length; i++) {
|
return new PdfFile(input.originalFilename, pdfDoc, RepresentationType.PDFLibDocument, input.filename+"_scaledPages");
|
||||||
resize(pages[i], pageSize[i]);
|
});
|
||||||
}
|
|
||||||
} else {
|
|
||||||
pages.forEach(page => { resize(page, pageSize) });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return new PdfFile(file.originalFilename, pdfDoc, RepresentationType.PDFLibDocument, file.filename+"_scaledPages");
|
static resize(page: PDFPage, newSize: {width?:number,height?:number}) {
|
||||||
}
|
const calculatedSize = ScalePage.calculateSize(page, newSize);
|
||||||
|
|
||||||
function resize(page: PDFPage, newSize: {width?:number,height?:number}) {
|
|
||||||
const calculatedSize = calculateSize(page, newSize);
|
|
||||||
const xRatio = calculatedSize.width / page.getWidth();
|
const xRatio = calculatedSize.width / page.getWidth();
|
||||||
const yRatio = calculatedSize.height / page.getHeight();
|
const yRatio = calculatedSize.height / page.getHeight();
|
||||||
|
|
||||||
@ -64,7 +60,7 @@ function resize(page: PDFPage, newSize: {width?:number,height?:number}) {
|
|||||||
page.scaleContent(xRatio, yRatio);
|
page.scaleContent(xRatio, yRatio);
|
||||||
}
|
}
|
||||||
|
|
||||||
function calculateSize(page: PDFPage, newSize: {width?:number,height?:number}): {width:number,height:number} {
|
static calculateSize(page: PDFPage, newSize: {width?:number,height?:number}): {width:number,height:number} {
|
||||||
if (!newSize.width && !newSize.height){
|
if (!newSize.width && !newSize.height){
|
||||||
throw new Error(`Sizes '${newSize}' cannot have null width and null height`);
|
throw new Error(`Sizes '${newSize}' cannot have null width and null height`);
|
||||||
} else if (!newSize.width && newSize.height) {
|
} else if (!newSize.width && newSize.height) {
|
||||||
@ -78,14 +74,4 @@ function calculateSize(page: PDFPage, newSize: {width?:number,height?:number}):
|
|||||||
}
|
}
|
||||||
return { width: newSize.width, height: newSize.height };
|
return { width: newSize.width, height: newSize.height };
|
||||||
}
|
}
|
||||||
|
|
||||||
export const PageSize = Object.freeze({
|
|
||||||
a4: {
|
|
||||||
width: 594.96,
|
|
||||||
height: 841.92
|
|
||||||
},
|
|
||||||
letter: {
|
|
||||||
width: 612,
|
|
||||||
height: 792
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user