首先我们要把页面连接放在ifram中,利用html2canvas和Canvas2Image对页面进行转换
<template>
<div style="height: 90vh;"><iframe src="XXX.html" frameborder="0" height="90%" id="frameid" width="100%"></iframe></div>
<div id="img" v-html="html"></div>
<button @click="toPic">下载图片</button>
script
import html2canvas from 'html2canvas'
import Canvas2Image from './canvas2image/index'
const toPic = () => {
const ifram: any = document.getElementById('frameid')
const testbody: any = ifram.contentWindow.document.getElementById('testbody'); // textbody为XXX.html的body的id名称
let canvas2 = document.createElement("canvas");
let w = parseInt(window.getComputedStyle(testbody).width);
let h = parseInt(window.getComputedStyle(testbody).height);
let context = canvas2.getContext("2d");
context?.scale(1,1);
canvas2.width = w;
canvas2.height = h;
html2canvas(testbody, {canvas: canvas2}).then((canvas: any) => {
const data = document.getElementById('img');
let img:any = Canvas2Image.convertToImage(canvas, w , h)
data?.appendChild(img)
// console.log(state.html)
})
}
Canvas2Image 下载地址 https://github.com/randreucetti/canvas2image
html2canvas 可以直接用npm安装: npm install html2canvas -S
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 提示词工程——AI应用必不可少的技术
· 字符编码:从基础到乱码解决
· 地球OL攻略 —— 某应届生求职总结