打印带有ant-design样式,直接选择dom用html转没有ant样式,只能是文字和排版
打印内容外层包裹dom
利用html2canvas转换dom为图片,用image标签引入canvas即可
<div id="modal-body-print">
打印的整个dom
</div>
print () {
this.contentH = 'auto'
const modalElement = document.getElementById('modal-body-print');
html2canvas(modalElement).then(function (canvas) {
setTimeout(() => {
let mywindow = window.open('', '', 'height=800,width=1000'); // 新的窗口
mywindow.document.write(`
<html>
<head>
<title></title>
<style type="text/css">
@page { margin: 0; } body{ padding: 20px; }
</style>
</head>
<body>`);
mywindow.document.write('<h1>' + document.title + '</h1>');
mywindow.document.write(`<br><img src="${canvas.toDataURL()}"/>`);
mywindow.document.write('</body></html>');
mywindow.document.close(); // IE >= 10 必要的兼容写法
mywindow.focus(); // IE >= 10*/ 必要的兼容写法
mywindow.print();
this.contentH = 150
}, 200)
});
},