Generate several images with html2canvas and convert to PDF

0
  • With the following function you can get a part of the HTML to convert to an image and save to PDF
  • Question What would be the form, in such a way that allows me to obtain several different IDs in document.body and generate several images and move to pdf?
  • html2canvas(document.body).then(function(canvas) {
                    var img = canvas.toDataURL('image/png');
                    var doc = new jsPDF();
                    doc.addImage(img, 'JPEG', 20, 20);
                    doc.save('test.pdf');
                });
        
    asked by Eze 17.11.2017 в 01:54
    source

    1 answer

    1

    I hope it's not too late, but I had the same problem and thanks to your doubt I solved it this way - >

    var img1 = '';
    var img2 = '';
    var doc = new jsPDF();
    html2canvas(document.querySelector("#content1")).then(canvas => {
       img1 = canvas.toDataURL('image/png');
       html2canvas(document.querySelector("#content2")).then(canvas => {
          img2 = canvas.toDataURL('image/png');
          doc.addImage(img1, 'JPEG', 20, 20);
          doc.addPage();//aqui hago un salto de pagina, una imagen en cada pagina
          doc.addImage(img2, 'JPEG', 20, 20);
          doc.save('test.pdf');
       });
    });
    
        
    answered by 18.05.2018 / 18:16
    source