html2canvas屏幕截图大小控制

时间:2018-12-25 06:48:00

标签: html2canvas

我使用截取屏幕截图中的代码并将其保存为pdf,我希望将屏幕截图的大小控制为400x700px。

<script type="text/javascript">

 function genPDF() {
    html2canvas(document.getElementById("all_content"), {
        onrendered: function (canvas) {
            var img = canvas.toDataURL("image/png");
            var doc = new jsPDF();
            doc.addImage(img, 'JPEG',20,20);
            doc.save("Businesscard-<?php echo $sss_name; ?>.pdf");
        }
    });
}


  </script>

1 个答案:

答案 0 :(得分:0)

添加高度宽度属性以获取所需的结果

function genPDF() {
    html2canvas(document.getElementById("all_content"), {
        onrendered: function (canvas) {
            canvas.setAttribute('width',400);       /*this->add*/
            canvas.setAttribute('height',700);      /*this->add*/
            var img = canvas.toDataURL("image/png");
            var doc = new jsPDF();
            doc.addImage(img, 'JPEG');
            doc.save("Businesscard-<?php echo $sss_name; ?>.pdf");
        }
    });
}