如何使用javascript将整个网页保存为PDF文件

时间:2018-04-11 10:56:36

标签: javascript jspdf

请问如何将整个网页保存为PDF文件。我已经有一个用于将网页保存为PDF文件的脚本,但它无法捕获整个网页。这是脚本:我的期望是保存网页的全部细节

   <script>
    function wa(){
            genPDF(0,0);
    }
    function genPDF(x,y){

        var doc = new jsPDF();
        //alert(document.getElementById("paper").clientWidth);//100.26
        var paperwidth = 8.5;
        var paperheight = 11;
        var ppi = parseFloat(document.getElementById("paper").clientWidth)/paperwidth;
        var height = ppi.toFixed(2)*paperheight;//instead of 11 inches
        var numpages = Math.ceil(parseFloat(document.getElementById("paper").clientHeight)/height.toFixed(2));

        window.scrollTo(0,0);
        html2canvas(document.getElementById("paper"),{
            onrendered:function (canvas){
                var x=0;
                while (x<numpages)
                {
                    var y = x*height;
                    window.scrollTo(0,y);
                    var img = canvas.toDataURL("image/png");
                    doc.addImage(img,'JPEG',5,5);
                    doc.addPage();
                    x=x+1;
                }
                doc.save('test.pdf');
            }
        });`enter code here`
    }
  </script>

谢谢

1 个答案:

答案 0 :(得分:0)

试试这个男人

  var pdf = new jsPDF('l', 'mm', [canvas.width, canvas.height]);

  var width = pdf.internal.pageSize.width;    
  var height = pdf.internal.pageSize.height;
  pdf.addImage(imgData, 'JPEG', 0, 0, width, height);
  pdf.save("download.pdf");