RavishaHesh / PDFJsAnnotations

Wrapper for PDF JS to add annotations
MIT License
322 stars 98 forks source link

Get real PDF coordinates #50

Closed Goddard closed 2 years ago

Goddard commented 2 years ago

I think this solution is great nice work.

The main problem I am currently facing is translating the annotation coordinates to actual PDF coordinates. Do you know the solution to this?

I have something like this viewport.convertToPdfPoint(fabricObjectItem.top, fabricObjectItem.left)

But I think this is not correct.

RavishaHesh commented 2 years ago

Can you describe your issue in little more detail? annotation positions are relative to the top left corner of each canvas(page)

Goddard commented 2 years ago

The PDF's real coordinates if I wanted to write the PDF are not the canvas PDF.

The canvas starts at the top left corner, but the PDF's coordinates start in the bottom right.

https://www.pdfscripting.com/public/PDF-Page-Coordinates.cfm#UserSpace

Goddard commented 2 years ago

Basically, you can't always use front end serialization of documents. You need to also include a solution so the backend which can do things in a more secure environment can properly write the annotations permanently after the user has marked the document.

I believe the pdf.js developers knew this and incldued a helper function, but I am unsure if it would be useful in this situation. viewport.convertToPdfPoint()

RavishaHesh commented 2 years ago

Okay, now I understand your requirement. I don't have a way to do this right now. I'll do a little bit of research and get back to you. I believe point conversion needs to be done using fabric.js rather than the PDF viewer(ex: after setting page size and other settings).

Goddard commented 2 years ago

Yeah you are right. You might need to get the entire page rectangle and subtract the size from the x or y before converting? I don't know.

RavishaHesh commented 2 years ago

X values seem okay. but you have to recalculate Y values with page height. Fabric.js fabric Pdf.js pdf

Goddard commented 2 years ago

Awesome mock up. So with those understandings you think that is all it takes to successfully write the PDF?

RavishaHesh commented 2 years ago

Awesome mock up. So with those understandings you think that is all it takes to successfully write the PDF?

Honestly, I can't confirm without testing it properly. But I'm a bit busy these days with my work and studies. I believe this can be use as a starting point

Goddard commented 2 years ago

For anyone curious I have finished testing and here are the things you need to take into account. Margins - personally I just set this to 0 when writing any thing I drew from the canvas. PDF Scale - the scale is calculated from the pdf width and height.

For the testing I did at least with this PHP library on the x axis it was divided by 210. For the y scale it was divided by 299. So 210 / $pdfWidth to get the x scale.

This gave pretty accurate results, but not saying this is perfect. I only tested with A4 pdf formats.

Hope this helps some one.