fix: auto orientation in Fix Page Size now matches source page layout and add tests

This commit is contained in:
alam00000
2026-04-04 13:16:27 +05:30
parent 4efab56e48
commit 38c42fd197
2 changed files with 271 additions and 7 deletions
+21 -7
View File
@@ -489,12 +489,26 @@ export async function fixPageSize(
const { width: sourceWidth, height: sourceHeight } = sourcePage.getSize();
const embeddedPage = await outputDoc.embedPage(sourcePage);
const outputPage = outputDoc.addPage([targetWidth, targetHeight]);
let pageTargetWidth = targetWidth;
let pageTargetHeight = targetHeight;
if (orientation === 'auto') {
const isSourceLandscape = sourceWidth > sourceHeight;
const isTargetLandscape = pageTargetWidth > pageTargetHeight;
if (isSourceLandscape !== isTargetLandscape) {
[pageTargetWidth, pageTargetHeight] = [
pageTargetHeight,
pageTargetWidth,
];
}
}
const outputPage = outputDoc.addPage([pageTargetWidth, pageTargetHeight]);
outputPage.drawRectangle({
x: 0,
y: 0,
width: targetWidth,
height: targetHeight,
width: pageTargetWidth,
height: pageTargetHeight,
color: rgb(
options.backgroundColor.r,
options.backgroundColor.g,
@@ -502,16 +516,16 @@ export async function fixPageSize(
),
});
const scaleX = targetWidth / sourceWidth;
const scaleY = targetHeight / sourceHeight;
const scaleX = pageTargetWidth / sourceWidth;
const scaleY = pageTargetHeight / sourceHeight;
const useFill = options.scalingMode.toLowerCase() === 'fill';
const scale = useFill ? Math.max(scaleX, scaleY) : Math.min(scaleX, scaleY);
const scaledWidth = sourceWidth * scale;
const scaledHeight = sourceHeight * scale;
const x = (targetWidth - scaledWidth) / 2;
const y = (targetHeight - scaledHeight) / 2;
const x = (pageTargetWidth - scaledWidth) / 2;
const y = (pageTargetHeight - scaledHeight) / 2;
outputPage.drawPage(embeddedPage, {
x,