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

Questions:

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,
        startY,
        selectedBoxes = [],
        $selectionMarquee = $('#selectionMarquee'),
        positionBox = function ($box, coordinates) {
            $box.css(
                'top', coordinates.top
            ).css(
                'left', coordinates.left
            ).css(
                'height', coordinates.bottom - coordinates.top
            ).css(
                '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;
        positionBox($selectionMarquee,
            getBoxCoordinates(startX, startY, startX, startY));
        $selectionMarquee.show();
        $(this).on('mousemove', trackMouse);
    }).on('mouseup', function (event) {
        var position,
            $selectedBox;

            $selectionMarquee.hide();

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

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

                positionBox($selectedBox, position);

                $selectedBox.show();

                selectedBoxes.push(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.