Home » Php » php – Plot coordinates on PDF displayed in iFrame

php – Plot coordinates on PDF displayed in iFrame

Posted by: admin July 12, 2020 Leave a comment


Firstly I appreciate my request is quite “ambitious”, but any help is greatly appreciated as I’m not sure the best way to proceed.

On my site (built with PHP/MySQL) after a user has uploaded a PDF I would like to display the PDF inline on the page (I’m assuming in an iFrame). I then need them to be able to drag out a number of “boxes” on top of the PDF (I’m assuming with jQuery). I then need to record the co-ordinates of this box so then later I can re-create the PDF injecting new text into the defined “boxes”.

Does this sound feasible? If not what else would you suggest? (please don’t say imagemagick!)

I know how to recreate a PDF injecting new text, but my issue is with how to allow the user to record those coordinates.

How to&Answers:

You could use PDF.js to render the PDF on the page. PDF.js will display it as part of the page so you can attach events and interact with it in ways you could not if it was being displayed by the Acrobat plugin.

I couldn’t find a preexisting library for getting the coordinates so I whipped up this code to implement it.

Live demo of selection code

$(function () {
    "use strict";
    var startX,
        selectedBoxes = [],
        $selectionMarquee = $('#selectionMarquee'),
        positionBox = function ($box, coordinates) {
                'top', coordinates.top
                'left', coordinates.left
                'height', coordinates.bottom - coordinates.top
                'width', coordinates.right - coordinates.left
        compareNumbers = function (a, b) {
            return a - b;
        getBoxCoordinates = function (startX, startY, endX, endY) {
            var x = [startX, endX].sort(compareNumbers),
                y = [startY, endY].sort(compareNumbers);

            return {
                top: y[0],
                left: x[0],
                right: x[1],
                bottom: y[1]
        trackMouse = function (event) {
            var position = getBoxCoordinates(startX, startY,
                event.pageX, event.pageY);
            positionBox($selectionMarquee, position);

    $(document).on('mousedown', function (event) {
        startX = event.pageX;
        startY = event.pageY;
            getBoxCoordinates(startX, startY, startX, startY));
        $(this).on('mousemove', trackMouse);
    }).on('mouseup', function (event) {
        var position,


            position = getBoxCoordinates(startX, startY,
                event.pageX, event.pageY);

            if (position.left !== position.right &&
            position.top !== position.bottom) {
                $selectedBox = $('<div class="selected-box"></div>');

                positionBox($selectedBox, position);



                $(this).off('mousemove', trackMouse);

You will have to tweak it to get coordinates that are relative to the PDF once you display it, but this should get you on the right track.