2023-10-26 20:53:02 +02:00
|
|
|
|
2023-10-26 22:41:48 +02:00
|
|
|
import { PDFDocument, ParseSpeeds } from 'pdf-lib';
|
2023-10-26 20:53:02 +02:00
|
|
|
|
|
|
|
export async function scaleContent(snapshot, scaleFactor) {
|
2023-10-16 23:11:33 +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-16 23:11:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const pages = pdfDoc.getPages();
|
|
|
|
|
|
|
|
pages.forEach(page => {
|
|
|
|
const width = page.getWidth();
|
|
|
|
const height = page.getHeight();
|
|
|
|
|
|
|
|
// Scale content
|
2023-10-22 00:55:28 +02:00
|
|
|
page.scaleContent(scaleFactor, scaleFactor);
|
2023-10-16 23:11:33 +02:00
|
|
|
const scaled_diff = {
|
2023-10-22 00:55:28 +02:00
|
|
|
width: Math.round(width - scaleFactor * width),
|
|
|
|
height: Math.round(height - scaleFactor * height),
|
2023-10-16 23:11:33 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Center content in new page format
|
|
|
|
page.translateContent(Math.round(scaled_diff.width / 2), Math.round(scaled_diff.height / 2));
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// Serialize the modified document
|
|
|
|
return pdfDoc.save();
|
|
|
|
};
|