Append Existing Pdf to Jspdf

Deadly 提交于 2019-12-12 08:16:10

问题


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');
    });


});

回答1:


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');
    });


来源:https://stackoverflow.com/questions/46891411/append-existing-pdf-to-jspdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!