How to do Jspdf dynamic positioning

Asked

Viewed 155 times

-1

I’m using the plugin jsPDF to generate a PDF. In my created method, I receive an array, I go through it, I try to write and position dynamically in the PDF, but I have no idea how to do this.

 $scope.gerarPdf = function (item) {
        var doc = new jsPDF();

        for (var i = 0; i < item.length; i++) {
            doc.text(item[i].nome, x, y);
            doc.text(item[i].validade,  x, y);
            doc.text(item[i].saldoParcial,  x, y);
        }
        doc.save('teste.pdf');
        return doc;
    };

Where possessed: 'x', 'y', is where I wanted to define positioning dynamically.

  • I did it in a project a while back, I took the initial value and I’m incrementing it according to the amount of records

  • I tried to do this using the 'i' of the interaction, but it came out misaligned..

  • You’re on the right track, now it’s just a matter of calculating right

1 answer

1

You are on the right track, it is only a matter of calculation. Look at this example:

var x = 15;
var y = 20;

for (var i = 0; i < item.length; i++) {
    doc.text(item[i].nome, x, y);
    doc.text(item[i].validade,  x, y+6);
    doc.text(item[i].saldoParcial,  x, y+12);
    y = y + 20
}

Doing this would already be put all the data one at the bottom of the other in the same column. To change column, just edit the X as well.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.