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

19 lines
535 B
TypeScript
Raw Normal View History

2023-10-26 20:53:02 +02:00
2023-10-26 22:41:48 +02:00
import { PDFDocument, ParseSpeeds, degrees } from 'pdf-lib';
2023-10-26 20:53:02 +02:00
export async function rotatePages(snapshot: string | Uint8Array | ArrayBuffer, rotation: number): Promise<Uint8Array> {
// Load the original PDF file
2023-10-26 22:41:48 +02:00
const pdfDoc = await PDFDocument.load(snapshot, {
parseSpeed: ParseSpeeds.Fastest,
});
const pages = pdfDoc.getPages();
pages.forEach(page => {
// Change page size
2023-10-26 22:41:48 +02:00
page.setRotation(degrees(rotation))
});
// Serialize the modified document
return pdfDoc.save();
};