JSPDF(HTML2Canvas)投掷错误“错误加载图像”与JPG(但不使用PNG)
我在升级一个使用旧版本的JSPDF(1.x)的网站上升级到最新版本(2.5.1)时遇到
了
问题 为“错误加载映像”或“错误加载背景图像”,
它遇到了一个jpeg文件,它只是失败了,如果我将加载到png的图像更改
则加载良好。这是隔离到我可以的最简单形式中的问题
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.js" integrity="sha512-sn/GHTj+FCxK5wam7k9w4gPPm6zss4Zwl/X9wgrvGMFbnedR8lTUSLdsolDRBRzsX6N+YgG6OWyvn9qaFVXH9w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
<img src="image.jpg" id="testImage">
<a href="javascript:download()">Download</a>
</body>
<script>
function download()
{
const { jsPDF } = window.jspdf;
pdf = new jsPDF("p", "mm", [ 980, 1387]);
pdf.html(
document.getElementById('testImage'),
{
html2canvas: {
// insert html2canvas options here, e.g.
logging: true
},
callback: function(){ pdf.save("a4.pdf");}
}
)
}
</script>
</html>```
I am having problems upgrading a website that uses an old version of jsPDF (1.x) to the latest version (2.5.1)
We use the .html() to render the HTML to the PDF
One issue we are getting is any time it comes across a jpeg file it simply fails with the error "Error loading image" or "Error loading background image"
If I change the image being loaded to a PNG it loads fine.
This is the issue isolated into the simplest form I could
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.js" integrity="sha512-sn/GHTj+FCxK5wam7k9w4gPPm6zss4Zwl/X9wgrvGMFbnedR8lTUSLdsolDRBRzsX6N+YgG6OWyvn9qaFVXH9w==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
</head>
<body>
<img src="image.jpg" id="testImage">
<a href="javascript:download()">Download</a>
</body>
<script>
function download()
{
const { jsPDF } = window.jspdf;
pdf = new jsPDF("p", "mm", [ 980, 1387]);
pdf.html(
document.getElementById('testImage'),
{
html2canvas: {
// insert html2canvas options here, e.g.
logging: true
},
callback: function(){ pdf.save("a4.pdf");}
}
)
}
</script>
</html>```
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我在异步函数中使用此结构。尝试一下。
I use this structure in my async function. Try it.