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

前端 未结 5 1958
生来不讨喜
生来不讨喜 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条回答
  •  猫巷女王i
    2021-02-19 05:55

    You can try it:

    $('#cmd2').click(function () {
    
        var len = 4; //$x(".//body/div/div").length
        var pdf = new jsPDF('p', 'mm','a4');
        var position = 0;
        Hide
        for (let i = 1;i  <= len; i++){
            html2canvas(document.querySelector('#pg'+i),
                {dpi: 300, // Set to 300 DPI
                scale: 1 // Adjusts your resolution
                }).then(canvas => {
                pdf.addImage(canvas.toDataURL("images/png", 1), 'PNG', 0,position, 210, 295);
    
                if (i == len){
                    pdf.save('sample-file.pdf');
                }else{
                    pdf.addPage();
                }
            });
        }
    });
    

提交回复
热议问题