1.先安装print.js
yarn add print-js
2.引入
<template>
<div>
//使用id
<div id="printBox">
//需要打印的内容
<div class="boxMain">
</div>
</div>
<el-button class="no-print" @click="printBtn" type="primary">打印预览</el-button>
</div>
</template>
<script setup lang="ts">
import printJS from 'print-js'
const printHtmlCss = () => {
printJS({
printable: 'printBox', // 'printFrom', // 标签元素id
type: 'html',
header: itemData.value.name, // '表单',
// css:'./index.css',//这个我试的一直没成功,不知道啥问题
//page-break-inside: avoid目的是防止图片之类的在页尾被截断,导致重叠问题
style:'@page{margin: 10mm 10mm;}.no-break{page-break-inside: avoid;}', // 需要打印的样式在这里调整(默认的样式很乱,需要调整)
ignoreElements: ['no-print'] // ['no-print'],有class="no-print"标识就不会被打印
})
}
</script>