Javascript jspdf到addImage:错误提供的数据不是JPEG
我正在尝试将图像添加到我的pdf:Javascript jspdf到addImage:错误提供的数据不是JPEG,javascript,angularjs,base64,jpeg,jspdf,Javascript,Angularjs,Base64,Jpeg,Jspdf,我正在尝试将图像添加到我的pdf: var image = '../images/example.jpg'; doc.addImage(image, 'JPEG', 0, 0, 700, 145); 我得到了这个错误: 错误:提供的数据不是JPEG格式 但是,当我添加base64映像时: var image = 'data:image/jpeg;base64,/9j/6GADS...' doc.addImage(image, 'JPEG', 0, 0, 700, 145); 很好用 为什么第
var image = '../images/example.jpg';
doc.addImage(image, 'JPEG', 0, 0, 700, 145);
我得到了这个错误:
错误:提供的数据不是JPEG格式
但是,当我添加base64映像时:
var image = 'data:image/jpeg;base64,/9j/6GADS...'
doc.addImage(image, 'JPEG', 0, 0, 700, 145);
很好用
为什么第一个版本不起作用?
我正在尝试:
var image = $base64.encode('../images/example.jpg')
同样的错误再次出现
这里发生了什么?解决方案是什么?您可以使用
我有这个确切的问题,但只在现场版本,而不是在我的本地版本。这怎么可能,缺少什么?我刚刚更新了文件,现在根本不起作用了。除了下面的解决方案,还有其他解决方案吗?我读的和上面类似,但是responsetype是blob
function toDataUrl(src, callback, outputFormat) {
// Create an Image object
var img = new Image();
// Add CORS approval to prevent a tainted canvas
img.crossOrigin = 'Anonymous';
img.onload = function() {
// Create an html canvas element
var canvas = document.createElement('CANVAS');
// Create a 2d context
var ctx = canvas.getContext('2d');
var dataURL;
// Resize the canavas to the image dimensions
canvas.height = this.height;
canvas.width = this.width;
// Draw the image to a canvas
ctx.drawImage(this, 0, 0);
// Convert the canvas to a data url
dataURL = canvas.toDataURL(outputFormat);
// Return the data url via callback
callback(dataURL);
// Mark the canvas to be ready for garbage
// collection
canvas = null;
};
// Load the image
img.src = src;
}