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

后端 未结 11 2094
醉话见心
醉话见心 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');
    
          }
        });
      },
    }
    <script src="Scripts/html2canvas.js"></script>
    <script src="Scripts/jsPDF/jsPDF.js"></script>
    <script src="Scripts/jsPDF/plugins/canvas.js"></script>
    <script src="Scripts/jsPDF/plugins/addimage.js"></script>
    <script src="Scripts/jsPDF/plugins/fileSaver.js"></script>
    <div id="my_mm" style="height: 1mm; display: none"></div>
    
    <div id="exportToPDF">
      Your html here.
    </div>
    
    <button id="export_btn" onclick="ExportModule.ExportToPDF();">Export</button>

    0 讨论(0)
  • 2020-12-04 22:17

    i faced same problem but i solve using this code

    html2canvas(body,{
                    onrendered:function(canvas){
                        var pdf=new jsPDF("p", "mm", "a4");
                        var width = pdf.internal.pageSize.getWidth();    
                        var height = pdf.internal.pageSize.getHeight();
                        pdf.addImage(canvas, 'JPEG', 0, 0,width,height);
                        pdf.save('test11.pdf');
                    }
                }) 
    
    0 讨论(0)
  • 2020-12-04 22:23

    You can get the width and height of PDF document like below,

    var doc = new jsPDF("p", "mm", "a4");
    
    var width = doc.internal.pageSize.getWidth();
    var height = doc.internal.pageSize.getHeight();
    

    Then you can use this width and height for your image to fit the entire PDF document.

    var imgData = 'data:image/jpeg;base64,/9j/4AAQSkZJ......';
    
    doc.addImage(imgData, 'JPEG', 0, 0, width, height);
    

    Make sure that your image has the same size (resolution) of the PDF document. Otherwise it will look distorted (stretched).

    If you want convert px to mm use this link http://www.endmemo.com/sconvert/millimeterpixel.php

    0 讨论(0)
  • 2020-12-04 22:24

    My answer deals with a more specific case of what you are asking but I think one could draw some ideas from this to apply more generally. Also, I would post this as a comment to Purushoth's answer (on which mine is based), if only I could.

    Ok, so my problem was how to fit a web page into the pdf document, without losing the aspect ratio. I used jsPDF in conjuction with html2canvas and I calculated the ratio from my div's width and height. I applied that same ratio to the pdf document and the page fit perfectly onto the page without any distortion.

    var divHeight = $('#div_id').height();
    var divWidth = $('#div_id').width();
    var ratio = divHeight / divWidth;
    html2canvas(document.getElementById("div_id"), {
         height: divHeight,
         width: divWidth,
         onrendered: function(canvas) {
              var image = canvas.toDataURL("image/jpeg");
              var doc = new jsPDF(); // using defaults: orientation=portrait, unit=mm, size=A4
              var width = doc.internal.pageSize.getWidth();    
              var height = doc.internal.pageSize.getHeight();
              height = ratio * width;
              doc.addImage(image, 'JPEG', 0, 0, width-20, height-10);
              doc.save('myPage.pdf'); //Download the rendered PDF.
         }
    });
    
    0 讨论(0)
  • 2020-12-04 22:24

    The API changed since this commit, using version 1.4.1 it's now

    var width = pdf.internal.pageSize.getWidth();
    var height = pdf.internal.pageSize.getHeight();
    
    0 讨论(0)
提交回复
热议问题