使用 html2canvas 以固定宽度捕获页面,无论屏幕尺寸如何
我使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢 CBroe,我确实使用屏幕外 iframe 让它工作。如下:
我确实需要在加载 iframe 并保存图像后删除 iframe,但基本功能似乎可以正常工作,屏幕尺寸不会发生任何奇怪的变化。
Thanks to CBroe I did get it working using an off screen iframe. As follows:
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.