Files
Stirling-PDF/shared-operations/src/functions/scalePage.ts

92 lines
3.3 KiB
TypeScript
Raw Normal View History

2023-10-26 21:53:02 +03:00
import Joi from "@stirling-tools/joi";
2024-01-04 20:17:54 -05:00
import { PDFPage } from "pdf-lib";
2024-02-23 23:55:29 +01:00
import { PdfFile, RepresentationType } from "../wrappers/PdfFile";
import { JoiPDFFileSchema } from "../wrappers/PdfFileJoi";
2023-11-19 01:19:57 +03:00
const whSchema = Joi.string().custom((value, helpers) => {
2024-01-04 20:17:54 -05:00
console.log("value.pageSize", typeof value);
2023-11-19 01:19:57 +03:00
try {
const obj = JSON.parse(value);
if (!obj.width && !obj.height) {
2024-01-04 20:17:54 -05:00
return helpers.error("any.required", { message: "At least one of width/height must be present" });
2023-11-19 01:19:57 +03:00
}
2024-01-04 20:17:54 -05:00
if (typeof obj.width != "number" && typeof obj.width != "undefined") {
return helpers.error("any.invalid", { message: "Width must be a number if present" });
2023-11-19 01:19:57 +03:00
}
2024-01-04 20:17:54 -05:00
if (typeof obj.height != "number" && typeof obj.height != "undefined") {
return helpers.error("any.invalid", { message: "Height must be a number if present" });
2023-11-19 01:19:57 +03:00
}
return obj;
} catch (error) {
2024-01-04 20:17:54 -05:00
return helpers.error("any.invalid", { message: "Value must be a valid JSON" });
2023-11-19 01:19:57 +03:00
}
});
export const ScalePageSchema = Joi.object({
file: JoiPDFFileSchema.required(),
2023-11-19 01:19:57 +03:00
pageSize: Joi.alternatives().try(whSchema, Joi.array().items(whSchema)).required(),
});
2023-10-26 21:53:02 +03:00
2024-01-04 20:17:54 -05:00
export interface ScalePageParamsType {
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;
2023-11-12 16:57:53 +03:00
const pages = pdfDoc.getPages();
2023-10-16 23:11:33 +02:00
2023-11-12 16:57:53 +03:00
if (Array.isArray(pageSize)) {
if (pageSize.length != pages.length) {
2024-01-04 20:17:54 -05:00
throw new Error(`Number of given sizes '${pageSize.length}' is not the same as the number of pages '${pages.length}'`);
2023-11-12 16:57:53 +03:00
}
for (let i=0; i<pageSize.length; i++) {
resize(pages[i], pageSize[i]);
}
} else {
2024-01-04 20:17:54 -05:00
pages.forEach(page => { resize(page, pageSize) });
2023-11-12 16:57:53 +03:00
}
return new PdfFile(file.originalFilename, pdfDoc, RepresentationType.PDFLibDocument, file.filename+"_scaledPages");
2024-01-04 20:17:54 -05:00
}
2023-10-16 23:11:33 +02:00
2023-11-12 16:57:53 +03:00
function resize(page: PDFPage, newSize: {width?:number,height?:number}) {
const calculatedSize = calculateSize(page, newSize);
const xRatio = calculatedSize.width / page.getWidth();
const yRatio = calculatedSize.height / page.getHeight();
2023-11-19 01:19:57 +03:00
page.setSize(calculatedSize.width, calculatedSize.height);
2023-11-12 16:57:53 +03:00
page.scaleContent(xRatio, yRatio);
}
2023-10-16 23:11:33 +02:00
2023-11-12 16:57:53 +03:00
function 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`);
} 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 };
}
2024-01-04 20:17:54 -05:00
return { width: newSize.width, height: newSize.height };
2023-11-12 16:57:53 +03:00
}
2023-10-16 23:11:33 +02:00
2023-11-12 16:57:53 +03:00
export const PageSize = Object.freeze({
2023-10-16 23:11:33 +02:00
a4: {
width: 594.96,
height: 841.92
},
letter: {
width: 612,
height: 792
}
});