How can I find PDF element coordinates using html? I've built a html page that shows a pdf (iframe) and I want to use the mouse-position to find x,y coordinates on the pdf? is that possible?
Here is my current procedure:
Converting an existing PDF file into PNG using ImageMagic
while specifying a
density
of 300 DPI.
Displaying the PNG as a background image while giving the user an option to create rectangles on that image [new divs]
Saving each div's coordinates [top/left] to the database
Creating a PDF while using the original PDF as a template using FPDI and TCPDF
and applying these coordinates to the PDF, but they are positioned badly and not directly on the PDF.
I know that my saved positions are in pixels
while the PDF library uses millimeters
but even when I do the conversion the rectangles are positioned badly.
The coordinate system on a PDF page is called User Space. This is a flat 2- dimensional space, just like a piece of paper. And in fact that's a good way to think about it. The units of User Space are called "points" and there are 72 points/inch.
Try running "Preflight..." in Acrobat and choosing PDF Analysis -> List page objects, grouped by type of object . If you locate the text objects within the results list, you will notice there is a position value (in points) within the Text Properties -> * Font section.
I have drawn a Rectangle and indicates where you can find the lower-left corner (with coordinate (llx, lly) ) and the upper-right corner (with coordinate (urx, ury) ). The sides of the rectangle are always in parallel with the X and the Y axis, hence you only need two coordinates to define the rectangle.
Assuming you can use the pdfinfo
tool available on *nix systems, you can determine the size of the PDF file in points
$ pdfinfo a.pdf
Creator: Writer
Producer: LibreOffice 5.1
CreationDate: Tue Jan 23 19:45:15 2018
Tagged: no
UserProperties: no
Suspects: no
Form: none
JavaScript: no
Pages: 1
Encrypted: no
Page size: 595 x 842 pts (A4)
Page rot: 0
File size: 36740 bytes
Optimized: no
PDF version: 1.4
A PDF of size WxH would render to (W * 300 / 72)px x (H * 300 / 72)px
at 300DPI. So the example above renders to 2479px x 3508px
PNG at 300DPI. Remember: The pixels are rounded to the nearest integer if the calculation results in decimals A single pixel here and there should not affect your final output anyway.
As to where the 72 comes from, go through: https://graphicdesign.stackexchange.com/questions/5859/all-pdfs-appear-to-be-72-dpi-no-matter-what
Finding the mouse position relative to the iframe should be an easy task. I am not answering that part because the question is only for the conversion.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With