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
|
|
|
|
2023-11-08 01:33:22 +01:00
|
|
|
export async function rotatePages(snapshot: string | Uint8Array | ArrayBuffer, rotation: number): Promise<Uint8Array> {
|
2023-10-22 00:55:28 +02:00
|
|
|
// Load the original PDF file
|
2023-10-26 22:41:48 +02:00
|
|
|
const pdfDoc = await PDFDocument.load(snapshot, {
|
|
|
|
parseSpeed: ParseSpeeds.Fastest,
|
2023-10-22 00:55:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const pages = pdfDoc.getPages();
|
|
|
|
|
|
|
|
pages.forEach(page => {
|
|
|
|
// Change page size
|
2023-10-26 22:41:48 +02:00
|
|
|
page.setRotation(degrees(rotation))
|
2023-10-22 00:55:28 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
// Serialize the modified document
|
|
|
|
return pdfDoc.save();
|
|
|
|
};
|