<div>
不需要打印的内容
</div>
<div id='formContent' ref="formContent">
需要打印的内容
</div>
<a-button type="primary" @click="print">打印</a-button>
import html2canvas from 'html2canvas';
import printJS from 'print-js';
const formContent=ref();
//将dom转图片然后打印图片,dom转图片使用html2Canvas,打印使用printjs插件
const print=()=>{
const targetDom = document.querySelector('#formContent');
html2canvas(formContent.value, {
backgroundColor: null,
useCORS: true,
height: targetDom.scrollHeight,
width: targetDom.scrollWidth,
}).then((canvas) => {
const url = canvas.toDataURL();
printJS({
printable: url,
type: 'image',
documentTitle: '打印图片',
targetStyles: ['*'],
});
});
};
window.print()实现打印指定内容——功能实现
于 2024-03-13 15:13:36 首次发布