首先安裝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同樣可以去網(wǎng)上找找js文件引入,這里就不提供了:)