【发布时间】:2020-01-15 00:39:59
【问题描述】:
我正在将图片上传到服务器,然后我的脚本将其转换为数据 URI。一切正常,在 setTimeOut 函数中,警报显示 dataURI 很好,但下一行中的 addImage 函数似乎没有将图片添加到 PDF 中。最后一行中的 addImage 函数添加另一个图像就好了。
我已经尝试了一切,不明白为什么 setTimeOut 块内的 addImage 函数不会将图像添加到 PDF。
直播版:http://www.course0001.com/fiverr/lifemax/004/
if( document.getElementById("field_file").files.length == 0 ){
doc.addImage(logoData, 'JPEG', 65, 35, 87, 27); // Add logo
} else {
let fnm = $('input[type=file]')[0].files[0].name;
let fileExtension = getExtension(fnm);
var logo = null;
getDataUri('/fiverr/lifemax/004/backend/logos/logo-'+uniqueFileName+'.'+fileExtension, function(dataUri) {
logo = dataUri;
console.log("logo=" + logo);
});
function getDataUri(url, cb)
{
var image = new Image();
image.setAttribute('crossOrigin', 'anonymous');
image.onload = function () {
var canvas = document.createElement('canvas');
canvas.width = this.naturalWidth;
canvas.height = this.naturalHeight;
//next three lines for white background in case png has a transparent background
var ctx = canvas.getContext('2d');
ctx.fillStyle = '#fff'; /// set white fill style
ctx.fillRect(0, 0, canvas.width, canvas.height);
canvas.getContext('2d').drawImage(this, 0, 0);
cb(canvas.toDataURL('image/jpeg'));
};
image.src = url;
}
setTimeout(function(){
alert(logo); // This displays the data URI fine
doc.addImage(logo, 'JPEG', 65, 35, 87, 27); // This never adds the image, however the alert displays the dataURI fine.
}, 3000);
}
doc.addImage(coverData, 'JPEG', 10, 95, 190, 190); // This adds the image just fine. Data URI is stored in a variable as string.
【问题讨论】:
标签: javascript base64 jspdf