运维日志您现在的位置是:首页 > 博客日志 > 运维日志

html2canvas 网页转图片 文字向下偏移解决方法

<a href='mailto:'>微wx笑</a>的头像微wx笑 2026-04-22运维日志 0 0关键字: html2canvas  截图  

注意:这是 html2canvas 1.4.1 遇到的问题,在0.5版本没有这个问题,但是生成的图片文字不清晰;有网友反馈 1.0.0 也不存在这个问题;https://unpkg.com/html2canvas@1.0.0/dist/html

注意:这是 html2canvas 1.4.1 遇到的问题,在0.5版本没有这个问题,但是生成的图片文字不清晰;CQH无知

有网友反馈 1.0.0 也不存在这个问题;CQH无知

https://unpkg.com/html2canvas@1.0.0/dist/html2canvas.js CQH无知

https://unpkg.com/html2canvas@1.0.0/dist/html2canvas.min.js CQH无知


CQH无知

https://cdn.jsdelivr.net/npm/html2canvas@1.4.1/dist/html2canvas.min.js CQH无知


CQH无知

新版本,还没测试过CQH无知

https://cdn.jsdelivr.net/npm/@html2canvas/html2canvas@1.6.3/dist/html2canvas.min.js CQH无知


CQH无知


CQH无知


CQH无知

解决方法:CQH无知

1、全局样式:CQH无知

img {display: initial;} CQH无知

2、换版本;CQH无知

3、DS给的答案:社区里最推荐的方法CQH无知

// 在调用 html2canvas 前执行
const style = document.createElement('style');
document.head.appendChild(style);
style.sheet?.insertRule('body > div:last-child img { display: inline-block; }');
// 调用 html2canvas 的代码...
// 截图完成后删除该样式
style.remove();

注意:这个方法只能这么用,设置为全局的样式 img { display: inline-block; } 没有效果;CQH无知


CQH无知

html2canvas 1.4.1 还会遇到一个问题:CQH无知

Element is not attached to a DocumentCQH无知

原因是调用的时候传递的是jquery对象;CQH无知


CQH无知

本文由 微wx笑 创作,采用 署名-非商业性使用-相同方式共享 4.0 许可协议,转载请附上原文出处链接及本声明。
原文链接:https://www.ivu4e.cn/blog/service/2026-04-22/2263.html

很赞哦! () 有话说 ()