Hopding / pdf-lib

Create and modify PDF documents in any JavaScript environment
https://pdf-lib.js.org
MIT License
6.93k stars 664 forks source link

Convert pixels in 1/72 inches #1428

Open stasmotorny opened 1 year ago

stasmotorny commented 1 year ago

What are you working on?

Hi, guys. I am working on documents signature implementation. It have few steps: 1) User see PDF file and should tap on the place where signature should be placed. On this step I'm using https://github.com/wonday/react-native-pdf this library to display document and take tap's coordinates. 2) After step 1 I navigate user to the screen where he should draw his signature. 3) After user draw signature I'm using pdf-lib drawImage() method to sign document.

There is few problems in this approach: 1) 0, 0 coordinates from react-native-pdf are in top left corner and as I understand coordinates are in pixels. 2) pdf-lib as I found out is using 1/72 inch as measurements units and 0, 0 point is in bottom left corner.

As result I can sign the document but with wrong placement of signature. So I need some way to properly convert coordinates taken from react-native-pdf to coordinates that are used by pdf-lib to place signature. I already tried a lot of variants but no one works fine. In current implementation I'm using formula like this: (pageHeight - signatureCoords.y) / (160 * PixelRatio.get()) / 72 So maybe someone can give me an advice how to properly place signature in PDF?

Additional Notes

No response

matheusPcruz18 commented 1 year ago

Hi, any positive progress with this feature? I'm currently struggling with the same problem

JohnLopen commented 10 months ago

Hi, I'm having the same problem as the first two guys, does anyone have a solution yet?

ziaKhan1995 commented 7 months ago

I am also struggling with the same issue. I want the user to drag an image on the pdf page, and on the confirm button insert the image on that location. I have values in terms of left and right of the absolute positioned image but when they are given as x and y of the drawImage method, it just disappears.