Skip to content

Commit ba6b0ce

Browse files
fix: position of image type widgets on landscape pdf are not correct
1 parent b99d3f6 commit ba6b0ce

File tree

1 file changed

+95
-4
lines changed

1 file changed

+95
-4
lines changed

apps/OpenSign/src/constant/Utils.js

Lines changed: 95 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,12 +1554,14 @@ export const multiSignEmbed = async (
15541554

15551555
radioGroup.enableReadOnly();
15561556
} else {
1557-
page.drawImage(img, {
1558-
x: xPos(position),
1559-
y: yPos(position),
1557+
const signature = {
1558+
x: position.xPosition,
1559+
y: position.yPosition,
15601560
width: scaleWidth,
15611561
height: scaleHeight
1562-
});
1562+
};
1563+
const imageOptions = getImagePosition(page, signature, 1);
1564+
page.drawImage(img, imageOptions);
15631565
}
15641566
});
15651567
}
@@ -2073,3 +2075,92 @@ export async function findContact(value) {
20732075
console.error("Error fetching suggestions:", error);
20742076
}
20752077
}
2078+
2079+
// `compensateRotation` is used to calculate x and y position of widget on portait, landscape pdf for pdf-lib
2080+
function compensateRotation(pageRotation, x, y, scale, dimensions, fontSize) {
2081+
//fontsize = imagewidth
2082+
let rotationRads = (pageRotation * Math.PI) / 180;
2083+
2084+
//These coords are now from bottom/left
2085+
let coordsFromBottomLeft = { x: x / scale };
2086+
if (pageRotation === 90 || pageRotation === 270) {
2087+
coordsFromBottomLeft.y = dimensions.width - (y + fontSize) / scale;
2088+
} else {
2089+
coordsFromBottomLeft.y = dimensions.height - (y + fontSize) / scale;
2090+
}
2091+
2092+
let drawX = null;
2093+
let drawY = null;
2094+
if (pageRotation === 90) {
2095+
drawX =
2096+
coordsFromBottomLeft.x * Math.cos(rotationRads) -
2097+
coordsFromBottomLeft.y * Math.sin(rotationRads) +
2098+
dimensions.width;
2099+
drawY =
2100+
coordsFromBottomLeft.x * Math.sin(rotationRads) +
2101+
coordsFromBottomLeft.y * Math.cos(rotationRads);
2102+
} else if (pageRotation === 180) {
2103+
drawX =
2104+
coordsFromBottomLeft.x * Math.cos(rotationRads) -
2105+
coordsFromBottomLeft.y * Math.sin(rotationRads) +
2106+
dimensions.width;
2107+
drawY =
2108+
coordsFromBottomLeft.x * Math.sin(rotationRads) +
2109+
coordsFromBottomLeft.y * Math.cos(rotationRads) +
2110+
dimensions.height;
2111+
} else if (pageRotation === 270) {
2112+
drawX =
2113+
coordsFromBottomLeft.x * Math.cos(rotationRads) -
2114+
coordsFromBottomLeft.y * Math.sin(rotationRads);
2115+
drawY =
2116+
coordsFromBottomLeft.x * Math.sin(rotationRads) +
2117+
coordsFromBottomLeft.y * Math.cos(rotationRads) +
2118+
dimensions.height;
2119+
} else {
2120+
//no rotation
2121+
drawX = coordsFromBottomLeft.x;
2122+
drawY = coordsFromBottomLeft.y;
2123+
}
2124+
return { x: drawX, y: drawY };
2125+
}
2126+
2127+
// `getImagePosition` is used to calulcate position of image type widget like x, y, width, height for pdf-lib
2128+
function getImagePosition(page, image, sizeRatio) {
2129+
let pageWidth;
2130+
// pageHeight;
2131+
if ([90, 270].includes(page.getRotation().angle)) {
2132+
pageWidth = page.getHeight();
2133+
// pageHeight = page.getWidth();
2134+
} else {
2135+
pageWidth = page.getWidth();
2136+
// pageHeight = page.getHeight();
2137+
}
2138+
2139+
// eslint-disable-next-line
2140+
if (!image?.hasOwnProperty("vpWidth")) {
2141+
image["vpWidth"] = pageWidth;
2142+
}
2143+
2144+
const pageRatio = pageWidth / (image.vpWidth * sizeRatio);
2145+
const imageWidth = image.width * sizeRatio * pageRatio;
2146+
const imageHeight = image.height * sizeRatio * pageRatio;
2147+
const imageX = image.x * sizeRatio * pageRatio;
2148+
const imageYFromTop = image.y * sizeRatio * pageRatio;
2149+
2150+
const correction = compensateRotation(
2151+
page.getRotation().angle,
2152+
imageX,
2153+
imageYFromTop,
2154+
1,
2155+
page.getSize(),
2156+
imageHeight
2157+
);
2158+
2159+
return {
2160+
width: imageWidth,
2161+
height: imageHeight,
2162+
x: correction.x,
2163+
y: correction.y,
2164+
rotate: page.getRotation()
2165+
};
2166+
}

0 commit comments

Comments
 (0)