How do I get a multi-page pdf from a website using jsPDF and HTML2Canvas?

前端 未结 5 1916
生来不讨喜
生来不讨喜 2021-02-19 05:03

I have a script that uses HTML2Canvas to take a screenshot of a div within the page, and then converts it to a pdf using jsPDF.

The problem is the pdf that

5条回答
  •  眼角桃花
    2021-02-19 05:55

    I was able to get it done using async functionality.

    (async function loop() {
        var pages = [...]
        for (var i = 0; i < pages.length; i++) {
          await new Promise(function(resolve) {
            html2canvas(pages[i], {scale: 1}).then(function(canvas) {
    
              var img=canvas.toDataURL("image/png");
              doc.addImage(img,'JPEG', 10, 10);
              if ((i+1) == pages.length) {
                doc.save('menu.pdf');
              } else {
                doc.addPage();
              }
              resolve();
            });
          });
        }
    })();
    

提交回复
热议问题