我正在使用jquery从类所有的img-tags中选择所有图片网址.my-images
然后我试图将它们放入带有jsPDF的pdf包文件中。像这样:
//Creating the PDF
var doc = new jsPDF()
//Getting all the images
var images = $('.my-images').map(function(){
return this.src;
}).get();
//Looping through the images and adding a page for each
var iterations = 1;
images.forEach(function(element) {
if(iterations > 1){
doc.addPage();
}
console.log(element);
doc.addImage(element, 'image/png', 10, 10, 190, 277);
iterations = ++iterations;
});
doc.save('a4.pdf')
所以现在我的问题。在我使用laravel-valet的地方,一切正常!真的很好甚至。
但当我把它推到服务器时,我得到:
未捕获的DOMException:无法在'Window'上执行'atob':要解码的字符串未正确编码。
我已经阅读了一些并尝试了atob and btoa然后它立即在本地打破...我没有发现有关这种奇怪行为的其他报道。关于如何进行的任何想法?
经过几个小时......我真的不知道问题究竟是什么......但我知道答案。您不需要直接从DOM选择中添加URL,而是需要实例化Image类。像这样:
//Creating the PDF
var doc = new jsPDF()
//Getting all the images
var images = $('.my-images').map(function(){
return this.src;
}).get();
//Looping through the images and adding a page for each
var iterations = 1;
images.forEach(function(element) {
if(iterations > 1){
doc.addPage();
}
var img = new Image();
//Then just add the url as a attribute
img.src = element;
//THEN add the image to the pdf
doc.addImage(img, 'image/png', 10, 10, 190, 277);
iterations = ++iterations;
});
doc.save('a4.pdf')
BOOM,你明白了。我想当你将它添加到Image类时,jsPDF喜欢的一些编码正在发生。