How to display an image in two pages in PDF using jsPDF?

前端 未结 6 1330
庸人自扰
庸人自扰 2020-12-14 05:03

I have a html page. In which I have a button , whenerever I click this button it will convert the entire html page into data image using html2canvas and placed it into PDF u

6条回答
  •  天命终不由人
    2020-12-14 05:26

    html2canvas($('#wrap')[0]).then(canvas => {
            try {
                contentH = $('#wrap').height();
                var img = canvas.toDataURL("image/png", 1.0);
                $w = $actw = canvas.width;
                $h = $acth = canvas.height;
                var pdf = new jsPDF("p", "mm", "a4");
                var width = $maxw = pdf.internal.pageSize.width;
                var height = $maxh = pdf.internal.pageSize.height;
                if (!$maxw) $maxw = width;
                if (!$maxh) $maxh = height;
                if ($w > $maxw) {
                    $w = $maxw;
                    $h = Math.round($acth / $actw * $maxw);
                }
                pdf.addImage(img, 'JPEG', 0, 0, $w, $h);
                $count = Math.ceil($h) / Math.ceil($maxh);
                $count = Math.ceil($count);
                for (var i = 1; i <= $count; i++) {
                    position = - $maxh * i
                    alert(position);
                    pdf.addPage(img, 'JPEG', 0, 0, $w, $h);
                    pdf.addImage(img, 'JPEG', 0, position, $w, $h);
                }
                pdf.save("cart.pdf");
            } catch (e) {
                alert("Error description: " + e.message);
            }
        });
    

提交回复
热议问题