How to set image to fit width of the page using jsPDF?

后端 未结 11 2242
醉话见心
醉话见心 2020-12-04 21:37

Is there any way to solve this? I tried to set width and height in mm. How can I set it to full-width?

11条回答
  •  孤街浪徒
    2020-12-04 22:15

    A better solution is to set the doc width/height using the aspect ratio of your image.

    var ExportModule = {
      // Member method to convert pixels to mm.
      pxTomm: function(px) {
        return Math.floor(px / $('#my_mm').height());
      },
      ExportToPDF: function() {
        var myCanvas = document.getElementById("exportToPDF");
    
        html2canvas(myCanvas, {
          onrendered: function(canvas) {
            var imgData = canvas.toDataURL(
              'image/jpeg', 1.0);
            //Get the original size of canvas/image
            var img_w = canvas.width;
            var img_h = canvas.height;
    
            //Convert to mm
            var doc_w = ExportModule.pxTomm(img_w);
            var doc_h = ExportModule.pxTomm(img_h);
            //Set doc size
            var doc = new jsPDF('l', 'mm', [doc_w, doc_h]);
    
            //set image height similar to doc size
            doc.addImage(imgData, 'JPG', 0, 0, doc_w, doc_h);
            var currentTime = new Date();
            doc.save('Dashboard_' + currentTime + '.pdf');
    
          }
        });
      },
    }
    
    
    
    
    
    
    
    
    Your html here.

提交回复
热议问题