JSPDF(HTML2Canvas)投掷错误“错误加载图像”与JPG(但不使用PNG)

发布于 2025-02-01 02:31:16 字数 1339 浏览 5 评论 0原文

我在升级一个使用旧版本的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2025-02-08 02:31:16

我在异步函数中使用此结构。尝试一下。

let scale_f = 0.7; // i scale my screenshots

let block = document.querySelector(".block");
    await html2canvas(block ,{
        scale: 1,
        windowWidth: 1110, // make screenshot like in setted window width 
        windowHeight: 2000, // same here but height
    }).then(canvas => {
        doc.addImage(canvas.toDataURL('image/png'), 'PNG', -30, 18, canvas.width*scale_f, canvas.height*scale_f);
    });

I use this structure in my async function. Try it.

let scale_f = 0.7; // i scale my screenshots

let block = document.querySelector(".block");
    await html2canvas(block ,{
        scale: 1,
        windowWidth: 1110, // make screenshot like in setted window width 
        windowHeight: 2000, // same here but height
    }).then(canvas => {
        doc.addImage(canvas.toDataURL('image/png'), 'PNG', -30, 18, canvas.width*scale_f, canvas.height*scale_f);
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文