image to base64 conversion

孤者浪人 提交于 2019-12-13 04:18:44

问题


I want to convert a images to base64 format for my chrome extension.

CODE:
var img_src = $('#elementId').attr('src');
converImgToBase64(img_src)
function convertImgToBase64(url)
{

    var canvas = document.createElement('CANVAS');
    ctx = canvas.getContext('2d');
    img = document.createElement('img'),
    img.src = url;
    img.onload = function()
    {
        canvas.height = img.height;
        canvas.width = img.width;


        var dataURL = canvas.toDataURL('image/png');
        alert(dataURL);

        var doc = new jsPDF();
        doc.addImage(dataURL, 'JPEG', 15, 40, 180, 160);
       doc.output('datauristring');
        canvas = null; 

    };
}

Alert box correctly shows the base64 format of image but PDF IS NOT GENERATING? How can i generate pdf for my image


回答1:


So your problem is not having an image in base64, but generating a pdf from it. So please, mind a little more you title next time ;)

By googling all these matters, I found this : http://parall.ax/products/jspdf which, i think, should help you.

As you can see, the homepage example look likes doing all that you need ;)



来源:https://stackoverflow.com/questions/24895044/image-to-base64-conversion

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!