Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/438.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jspdf到addImage:错误提供的数据不是JPEG_Javascript_Angularjs_Base64_Jpeg_Jspdf - Fatal编程技术网

Javascript jspdf到addImage:错误提供的数据不是JPEG

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); 很好用 为什么第

我正在尝试将图像添加到我的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 = $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;
}