Add image in pdf using jspdf

前端 未结 12 711
离开以前
离开以前 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:39

    For result in base64, before convert to canvas:

    var getBase64ImageUrl = function(url, callback, mine) {
    
                        var img = new Image();
    
                        url = url.replace("http://","//");
    
                        img.setAttribute('crossOrigin', 'anonymous');
    
                        img.onload = function () {
                            var canvas = document.createElement("canvas");
                            canvas.width =this.width;
                            canvas.height =this.height;
    
                            var ctx = canvas.getContext("2d");
                            ctx.drawImage(this, 0, 0);
    
                            var dataURL = canvas.toDataURL(mine || "image/jpeg");
    
                            callback(dataURL);
                        };
                        img.src = url;
    
                        img.onerror = function(){
    
                            console.log('on error')
                            callback('');
    
                        }
    
                }
    
       getBase64ImageUrl('Koala.jpeg', function(img){
             //img is a base64encode result
             //return img;
              console.log(img);
    
              var doc = new jsPDF();
                  doc.setFontSize(40);
                  doc.text(30, 20, 'Hello world!');
                  doc.output('datauri');
                  doc.addImage(img, 'JPEG', 15, 40, 180, 160);
        });
    

提交回复
热议问题