Add image in pdf using jspdf

前端 未结 12 703
离开以前
离开以前 2020-12-05 06:49

I am using jspdf to convert an image into a PDF.

I have converted the image into a URI using base64encode. But the problem is that there are no errors or warnings sh

12条回答
  •  清歌不尽
    2020-12-05 07:31

    maybe a little bit late, but I come to this situation recently and found a simple solution, 2 functions are needed.

    1. load the image.

      function getImgFromUrl(logo_url, callback) {
          var img = new Image();
          img.src = logo_url;
          img.onload = function () {
              callback(img);
          };
      } 
      
    2. in onload event on first step, make a callback to use the jspdf doc.

      function generatePDF(img){
          var options = {orientation: 'p', unit: 'mm', format: custom};
          var doc = new jsPDF(options);
          doc.addImage(img, 'JPEG', 0, 0, 100, 50);}
      
    3. use the above functions.

      var logo_url = "/images/logo.jpg";
      getImgFromUrl(logo_url, function (img) {
          generatePDF(img);
      });
      

提交回复
热议问题