Migrated more functions to use PdfFile

This commit is contained in:
Saud Fatayerji
2023-11-12 16:57:53 +03:00
parent 0d915fcc33
commit 8018947353
10 changed files with 153 additions and 94 deletions

View File

@@ -1,19 +1,26 @@
import { PDFDocument, ParseSpeeds, degrees } from 'pdf-lib';
export async function rotatePages(snapshot: string | Uint8Array | ArrayBuffer, rotation: number): Promise<Uint8Array> {
// Load the original PDF file
const pdfDoc = await PDFDocument.load(snapshot, {
parseSpeed: ParseSpeeds.Fastest,
});
import { degrees } from 'pdf-lib';
import { PdfFile, fromPdfLib } from '../wrappers/PdfFile';
export async function rotatePages(file: PdfFile, rotation: number|number[]): Promise<PdfFile> {
const pdfDoc = await file.getAsPdfLib();
const pages = pdfDoc.getPages();
pages.forEach(page => {
// Change page size
page.setRotation(degrees(rotation))
});
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))
});
}
// Serialize the modified document
return pdfDoc.save();
return fromPdfLib(pdfDoc, file.filename);
};