js将html转为图片主要是通过html2canvas这个插件去操作的。具体实现如下:
html部分:
<!--index.html--> <div id="container"> this is a test page!!! <i></i> <button onclick="download()">download</button> </div> <script src="./bundle.js"></script>
javascript部分:
//index.js import ‘./index.scss‘; import html2canvas from ‘html2canvas‘ window.download = () => { let container = document.getElementById(‘container‘) console.log(container) html2canvas(container).then(canvas => { let imgData = canvas.toDataURL() let link = document.createElement(‘a‘) link.href = imgData link.download = ‘test.png‘ link.click() }) }
利用html5的download属性实现文件下载
时间: 2024-10-10 13:24:08