简体   繁体   English

如何使用节点 js 将图像放置在 pdf 的右上角?

[英]How to place image on right corner of pdf using node js?

I have one pdf file and one image file which is the signature of the client.我有一个 pdf 文件和一个作为客户端签名的图像文件。 I need to place the client signature image on the top right corner of every page of pdf.我需要把客户端签名图片放在pdf每一页的右上角。 This whole process is in node js so I have used the pdf-lib npm package to attach image on pdf.整个过程都在节点 js 中,所以我使用了 pdf-lib npm package 将图像附加到 pdf 上。 The current issue I'm unable to place the image on the top-right corner.当前问题我无法将图像放在右上角。

Below is the logic which I used to set an image on the top-right corner of the pdf but this logic is not true for every case in some cases when the width of the image or pdf changes then it's not worked as expected.下面是我用来在 pdf 的右上角设置图像的逻辑,但在某些情况下,当图像的宽度或 pdf 发生变化时,此逻辑并非适用于所有情况,然后它没有按预期工作。 Sometimes images are getting too much small or sometimes too much large.Just because I set fix height and width.有时图像变得太小或有时太大。只是因为我设置了固定高度和宽度。 As I don't know how to calculate it dynamically因为我不知道如何动态计算它

在此处输入图像描述

        const pages = pdfDoc.getPages();
        for (let i = 0; i < pdfDoc.getPageCount(); i++) {
            let imagePage='';
            imagePage = pdfDoc.getPage(i);
            console.log(i+1)
            console.log(imagePage.getWidth())
            let xx=imagePage.getWidth()
            console.log(imagePage.getHeight())
            console.log(img.width)
            console.log(img.height)
            let yy=imagePage.getHeight()
            imagePage.drawImage(img, {
                x: xx-150,
                y: yy-70,
                width: 70,
                height: 70
            })
        }

在此处输入图像描述

Please check console values below请检查下面的控制台值

595.5 ​​​​​at ​​​​​​​​imagePage.getWidth()​​​ 

842.2499787 ​​​​​at ​​​​​​​​imagePage.getHeight()​​​

1200 ​​​​​at ​​​​​​​​img.width​​​

700 ​​​​​at ​​​​​​​​img.height

please check the above image where I attached the signature to pdf but in this case, the signature image is not properly visible so how can I set the image to pdf on the top-right corner?请检查上面的图像,我将签名附加到 pdf 但在这种情况下,签名图像不正确可见,那么如何将图像设置为右上角的 pdf? How can I calculate the x and y position dynamically for any pdf?如何动态计算任何 pdf 的 x 和 y position?

I got the solution to get dynamic x and y values to set an image on the top-right corner of the pdf我得到了获取动态 x 和 y 值以在 pdf 的右上角设置图像的解决方案

let xx=imagePage.getWidth();
console.log(imagePage.getHeight());
console.log("[PAGE_HEIGHT]",imagePage.getHeight());
const imgHeight = img.height;
const imgWidth = img.width;
console.log("[SIGNATURE_IMAGE_WIDTH]",img.width);
console.log("[SIGNATURE_IMAGE_HEIGHT]",img.height);
const marginX = 50;
let yy=imagePage.getHeight();
imagePage.drawImage(img, {
    x: xx -marginX-imgWidth,
    y: yy- marginX-imgHeight,
    width: imgWidth,
    height: imgHeight
})

margin is a fixed value to move an image from the top and right sides to get a proper view.边距是从顶部和右侧移动图像以获得正确视图的固定值。 I have removed image height and width from the actual page width and height that will return top right corner.我已经从将返回右上角的实际页面宽度和高度中删除了图像高度和宽度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM