javascript - jsPDF and image loading - Stack Overflow

I'm tryna use the jsPDF library. I'd like to load and insert an image, and export the PDF fil

I'm tryna use the jsPDF library. I'd like to load and insert an image, and export the PDF file.

My issue is about the image loading. I'm doing this: var imageData = getBase64Image('thinking-monkey.jpg'); and I should get the dataURL in base64 inside imageData.

My getBase64Image() function does the following:

function getBase64Image(url) {
    var img = new Image();
    var dataURL;

    img.src = url;

    img.onload = function() {
        var canvas = document.createElement('canvas');
        canvas.width = img.width;
        canvas.height = img.height;

        var context = canvas.getContext('2d');
        context.drawImage(img, 0, 0);

        dataURL = canvas.toDataURL('image/jpeg');

    }

    return dataURL;
}

But it returns 'undefined', because the image is like 65 Kb and doesn't load up at once. So when at return dataURL; the variable is still undefined.

I've tried to add a setTimeout() right before return dataURL; but it doesn't seem to be working.

How can I wait until the image is fully loaded to return dataURL?

Thanks.

I'm tryna use the jsPDF library. I'd like to load and insert an image, and export the PDF file.

My issue is about the image loading. I'm doing this: var imageData = getBase64Image('thinking-monkey.jpg'); and I should get the dataURL in base64 inside imageData.

My getBase64Image() function does the following:

function getBase64Image(url) {
    var img = new Image();
    var dataURL;

    img.src = url;

    img.onload = function() {
        var canvas = document.createElement('canvas');
        canvas.width = img.width;
        canvas.height = img.height;

        var context = canvas.getContext('2d');
        context.drawImage(img, 0, 0);

        dataURL = canvas.toDataURL('image/jpeg');

    }

    return dataURL;
}

But it returns 'undefined', because the image is like 65 Kb and doesn't load up at once. So when at return dataURL; the variable is still undefined.

I've tried to add a setTimeout() right before return dataURL; but it doesn't seem to be working.

How can I wait until the image is fully loaded to return dataURL?

Thanks.

Share asked Aug 28, 2013 at 10:22 gaelgael 551 gold badge1 silver badge8 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 2

You can use a callback. Pass the code that you want to execute after the image is fully loaded to the getBase64Image function, and execute it in the .onLoad function.

it would be something like this. (generatePdf is a function)

function getBase64Image(url,generatePdf) {
    var img = new Image();
    var dataURL;

    img.src = url;

    img.onload = function() {
        var canvas = document.createElement('canvas');
        canvas.width = img.width;
        canvas.height = img.height;

        var context = canvas.getContext('2d');
        context.drawImage(img, 0, 0);

        dataURL = canvas.toDataURL('image/jpeg');
        generatePdf(dataUrl);
    }
}
var generatePdf= function generatePdf(imageData){
   /** this function receives the image param and creates the pdf with it**/
   var doc = new jsPDF();
   doc.addImage(imageData, "JPEG", 60,50); 
   doc.save("test.pdf");
};

function generateImagePdf(url){ 
    getBase64Image(url, generatePdf);
}
//now call the generateImagePdf function, which will call the generateBase64Image function and wait for tyhe image to load to call the generatePdf function with the param
generateImagePdf("imageurl.jpg") ;

Move the "return" statement inside the img.onload function, this should do it I suspect.

function getBase64Image(url) {
    var img = new Image();
    var dataURL;

    img.src = url;

    img.onload = function() {
        var canvas = document.createElement('canvas');
        canvas.width = img.width;
        canvas.height = img.height;

        var context = canvas.getContext('2d');
        context.drawImage(img, 0, 0);

        dataURL = canvas.toDataURL('image/jpeg');

        return dataURL;   /* MOVED */
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745046849a4608151.html

相关推荐

  • javascript - jsPDF and image loading - Stack Overflow

    I'm tryna use the jsPDF library. I'd like to load and insert an image, and export the PDF fil

    7小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信