Add image in pdf using jspdf

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

    First you need to load the image, convert data, and then pass to jspdf (in typescript):

    loadImage(imagePath): ng.IPromise {
        var defer = this.q.defer();
        var img = new Image();
        img.src = imagePath;
        img.addEventListener('load',()=>{
                var canvas = document.createElement('canvas');
                canvas.width = img.width;
                canvas.height = img.height;
    
                var context = canvas.getContext('2d');
                context.drawImage(img, 0, 0);
    
                var dataURL = canvas.toDataURL('image/jpeg');
    
                defer.resolve(dataURL);
        });
    
        return defer.promise;
    }
    
    generatePdf() {
        this.loadImage('img/businessLogo.jpg').then((data) => {
            var pdf = new jsPDF();
            pdf.addImage(data,'JPEG', 15, 40, 180, 160);
            pdf.text(30, 20, 'Hello world!');
            var pdf_container =  angular.element(document.getElementById('pdf_preview'));
            pdf_container.attr('src', pdf.output('datauristring'));
        });
    }
    

提交回复
热议问题