html导出图片和打印
打印用js自带的print即可,导出图片需要引入http://image.niunan.net/html2canvas.min.js
<script src="html2canvas.min.js"></script> <script type="text/javascript"> function printContent() { var content = document.getElementById('print-content').innerHTML; var newWindow = window.open('', '打印窗口'); newWindow.document.write('<html><head><title>打印页面</title></head><body>' + content + '</body></html>'); newWindow.document.close(); newWindow.print(); newWindow.close(); } function saveImg() { // 选择 HTML 元素 let element = document.getElementById("print-content"); html2canvas(element, { allowTaint: true, scale: 2, background: "#F5F5F5" }).then(function (canvas) { document.body.appendChild(canvas); let dataURL = canvas.toDataURL(); let a = document.createElement("a"); a.setAttribute("download", "aaa.png"); a.setAttribute("href", dataURL); a.click(); document.body.removeChild(canvas) }); } </script>
标签:canvas,打印,导出,newWindow,content,html,print,document From: https://www.cnblogs.com/niunan/p/17552904.html