首先安装html2canvas
npm install html2canvas --save
然后引入html2canvas
import html2canvas from 'html2canvas'
打印
function print(elemId) {
html2canvas(document.getElementById(elemId), {
allowTaint: false,
useCORS: true,
}).then(canvas => {
//将图片保存到变量
let image = canvas.toDataURL("image/jpeg");
let img = document.createElement('img');
img.src = image
img.style.width = '100vw'
img.style.height = `${100 * img.height / img.width}vw`
var h = window.open('打印窗口', "_blank");
h.document.write($(img).prop("outerHTML"));
h.document.close();
setTimeout(() => {
h.print();
h.close();
}, 1000)
});
}
引入html2canvas同样可以去网上找找js文件引入,这里就不提供了:)
网友评论