方法一、
在项目中安装插件vue-print-nb
npm install vue-print-nb --save
在main.js中引入
import Print from 'vue-print-nb' //打印
Vue.use(Print)
使用
//点击打印
<el-button v-print="'#printMe'">打印</el-button>
//要打印的div
<div class="index-home" id="printMe"></div>
方法二、
在项目中安装插件html2canvas 和 printJS
npm install html2canvas --save
npm install print-js --save
在要打印的页面中引入
import html2canvas from 'html2canvas'
import printJS from 'print-js'
//打印按钮v-print
<el-button class="print-btn" v-print="printObj">打 印</el-button>
<div class="index-home" id="printMe"></div>
在data里面设置打印的DIV的id和标题
printObj: {
id: 'printMe',//打印标签的id
popTitle: ''// 打印的标题,因为css中写了去掉页眉页脚所以不显示
},
或者------
<el-button class="print-btn" @click="printHandle">打 印</el-button>
<div class="index-home" ref="printMe"></div>
printHandle() {
html2canvas(this.$refs.pdf, {
backgroundColor: null,
useCORS: true,
windowHeight: document.body.scrollHeight
}).then((canvas) => {
const url = canvas.toDataURL()
this.img = url
const style = '@page {margin:0 10mm};'
printJS({
printable: url,
type: 'image',
style,
documentTitle: ''
})
})
},
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?