使用 html2canvas 以固定宽度捕获页面,无论屏幕尺寸如何

发布于 2025-01-09 09:37:31 字数 1532 浏览 2 评论 0原文

我使用 html2canvas 将网页保存为图像,我希望在移动设备和 PC 上获得相同的结果。我的网页有一个表格,在较小的屏幕上呈现不同的效果(通过 CSS)。我希望 html2canvas 保存画布,就好像网页始终在 PC 屏幕上一样。这样,网页上的表格将始终看起来相同。

我目前有一个解决方法,在 html2canvas 运行之前临时将主体宽度和视口初始比例设置为更大,保存网页,然后恢复到之前的主体宽度和视口比例。我使用的代码位于这篇文章的底部。

它基本上可以工作,但用户体验很差,因为在 html2canvas 操作期间,网页会变大,暂停一会儿(在保存期间),然后恢复回来。从用户的角度来看,这看起来不太好。此外,这并不总是适用于所有移动设备。

有更好的方法吗?我是否可以拥有某种屏幕外 html 来镜像我的屏幕上 html,但始终会像在 PC 上一样呈现?

// adjust screen and viewport to allow all table elements to fit on the screen correctly
document.body.style.width = '1024px';
let viewport = document.querySelector("meta[name=viewport]");
let remembered_viewport = JSON.stringify(viewport.outerHTML)
viewport.setAttribute('content', 'width=device-width, initial-scale=2');

// use html2canvas to save the webpage
let tag = document.getElementById("all");
html2canvas(tag).then(function(canvas) {
    let link = document.createElement("a");
    link.download = filename.toLowerCase();
    canvas.toBlob( function(blob) {
            link.href = URL.createObjectURL(blob);
            link.click();
        }, 'image/jpg');
    });

// reset screen and viewport
document.body.style.width = "auto";
if (remembered_viewport.includes("initial-scale=0.5")) viewport.setAttribute('content', 'width=device-width, initial-scale=0.5');
if (remembered_viewport.includes("initial-scale=2")) viewport.setAttribute('content', 'width=device-width, initial-scale=2');
else viewport.setAttribute('content', 'width=device-width, initial-scale=1');

预先感谢,杰森。

I use html2canvas to save a webpage as an image and i'd like to get the same results on a mobile and a PC. My webpage has a table that renders differently on a smaller screen (via CSS). I would like html2canvas to save the canvas as if the webpage is always on a PC screen. This way the table on the webpage will always look the same.

I currently have a workaround which temporarily sets the body width and viewport initial-scale to something larger before html2canvas runs, saves the webpage, then reverts back to the previous body width and viewport scale. The code I use is at the bottom of this post.

It mostly works but has a bad user experience because during the html2canvas operation the webpage will grow larger, pause for a bit (during the save) then revert back. Which doesn't look very good from the users point of view. Also, this doesn't always work for every mobile device.

Is there a better way to do this? Can I have some sort of off screen html that mirrors my on screen html but will always render as if its on a PC?

// adjust screen and viewport to allow all table elements to fit on the screen correctly
document.body.style.width = '1024px';
let viewport = document.querySelector("meta[name=viewport]");
let remembered_viewport = JSON.stringify(viewport.outerHTML)
viewport.setAttribute('content', 'width=device-width, initial-scale=2');

// use html2canvas to save the webpage
let tag = document.getElementById("all");
html2canvas(tag).then(function(canvas) {
    let link = document.createElement("a");
    link.download = filename.toLowerCase();
    canvas.toBlob( function(blob) {
            link.href = URL.createObjectURL(blob);
            link.click();
        }, 'image/jpg');
    });

// reset screen and viewport
document.body.style.width = "auto";
if (remembered_viewport.includes("initial-scale=0.5")) viewport.setAttribute('content', 'width=device-width, initial-scale=0.5');
if (remembered_viewport.includes("initial-scale=2")) viewport.setAttribute('content', 'width=device-width, initial-scale=2');
else viewport.setAttribute('content', 'width=device-width, initial-scale=1');

Thanks in advance, Jason.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

无边思念无边月 2025-01-16 09:37:32

感谢 CBroe,我确实使用屏幕外 iframe 让它工作。如下:

// capture my on screen html
let html = document.documentElement.outerHTML;
// create the iframe
// there is css to push it off screen (ie. left: -1024px;)
let iframe = document.createElement("iframe");
iframe.style.width = "1024px";
iframe.style.height = "100%";
document.body.appendChild(iframe);
// add html to iframe
iframe.srcdoc = html;

// wait for iframe to load
iframe.addEventListener("load", () => {
   // use html2canvas to save the webpage
   html2canvas(iframe.contentWindow.document.body).then(function(canvas) {
     let filename = "image.jpg";
     let link = document.createElement("a");
     link.download = filename.toLowerCase();
     canvas.toBlob( function(blob) {
            link.href = URL.createObjectURL(blob);
            link.click();
        }, 'image/jpg');
     });
});

我确实需要在加载 iframe 并保存图像后删除 iframe,但基本功能似乎可以正常工作,屏幕尺寸不会发生任何奇怪的变化。

Thanks to CBroe I did get it working using an off screen iframe. As follows:

// capture my on screen html
let html = document.documentElement.outerHTML;
// create the iframe
// there is css to push it off screen (ie. left: -1024px;)
let iframe = document.createElement("iframe");
iframe.style.width = "1024px";
iframe.style.height = "100%";
document.body.appendChild(iframe);
// add html to iframe
iframe.srcdoc = html;

// wait for iframe to load
iframe.addEventListener("load", () => {
   // use html2canvas to save the webpage
   html2canvas(iframe.contentWindow.document.body).then(function(canvas) {
     let filename = "image.jpg";
     let link = document.createElement("a");
     link.download = filename.toLowerCase();
     canvas.toBlob( function(blob) {
            link.href = URL.createObjectURL(blob);
            link.click();
        }, 'image/jpg');
     });
});

I do need to remove the iframe after the iframe is loaded AND the image is saved, but the basics appear to work without any odd on screen size changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文