Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Append Existing Pdf to Jspdf

Tags:

pdf

jspdf

I am using the jspdf library to create a pdf and its working out great. I am now trying to append to that pdf another existing pdf. At the moment when my user clicks the download button it fires off two separate downloads. I was thinking that a work around might be creating two images and adding them to my pdf created with Jspdf. Has anyone appended an existing pdf to a pdf generated using jspdf?

$(document).ready(function () {
    var doc = new jsPDF('p', 'pt', 'letter');
    var imgData = 'cats.jpg'
  var specialElementHandlers = {
        '#content': function (element, renderer) {
            return true;
        }
    };
    $('#cmd').click(function () {
        doc.addImage(imgData, 'JPEG', 0, 250, 615, 200);
        doc.fromHTML($('#content').get(0), 0, 0, {

            'elementHandlers': specialElementHandlers
        });

        doc.save('TemporaryIdCard.pdf');
    });


});
like image 838
Leonardo Wildt Avatar asked Nov 07 '22 15:11

Leonardo Wildt


1 Answers

I ended up hacking an answer from here. Not thrilled about it but it works. I created images from the content in the PDF I was trying to append and then added each as a page to my doc

var doc = new jsPDF('p', 'pt', 'letter');
var imgData = 'cats.jpeg';
var imgData2 = 'dogs.jpeg';
var imgData3 = 'kittens.jpeg';    
var specialElementHandlers = {
        '#content': function (element, renderer) {
            return true;
        }
    };
    var pageHeight = doc.internal.pageSize.height;
    var y = 800;
    var x = 800;
    $('#cmd').click(function () {
        doc.addImage(imgData, 'JPEG', 0, 250, 615, 200);
        doc.fromHTML($('#content').get(0), 0, 0, {
            'elementHandlers': specialElementHandlers
        });
        if (y >= pageHeight) {
            doc.addPage();
            doc.addImage(imgData3, 'JPEG', 45, 45, 500, 550);
            y = 0;
        }
        if (x >= pageHeight) {
            doc.addPage();
            doc.addImage(imgData2, 'JPEG', 50, 70, 500, 500);
            x = 0;
        }
        doc.save('TemporaryIdCard.pdf');
    });
like image 76
Leonardo Wildt Avatar answered Nov 15 '22 09:11

Leonardo Wildt