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

32 lines
1002 B
TypeScript
Raw Normal View History

2023-10-26 21:53:02 +03:00
2023-11-12 16:57:53 +03:00
import { degrees } from 'pdf-lib';
2023-11-14 20:22:37 +01:00
import { PdfFile } from '../wrappers/PdfFile';
export type RotateParamsType = {
file: PdfFile;
rotation: number|number[];
}
export async function rotatePages(params: RotateParamsType): Promise<PdfFile> {
const { file, rotation } = params;
const pages = (await file.pdfLibDocument).getPages();
2023-11-12 16:57:53 +03:00
if (Array.isArray(rotation)) {
if (rotation.length != pages.length) {
throw new Error(`Number of given rotations '${rotation.length}' is not the same as the number of pages '${pages.length}'`)
}
for (let i=0; i<rotation.length; i++) {
const oldRotation = pages[i].getRotation().angle
pages[i].setRotation(degrees(oldRotation + rotation[i]))
}
} else {
pages.forEach(page => {
// Change page size
const oldRotation = page.getRotation().angle
page.setRotation(degrees(oldRotation + rotation))
});
}
2023-11-14 20:22:37 +01:00
return file;
};