赞
踩
任务:解决vue页面局部打印功能
print.js插件,可以打印 html、pdf、json数据
官网:https://printjs.crabbly.com/
一个整理的很好的讲解:https://www.jianshu.com/p/bc079fbb20c7
npm install print-js --save
import print from 'print-js'
以下作为测试代码,测试了表循环data中的数据、图片、echart图表。以下是打印区域
<div id="printBox"> <h3>打印预览</h3> <div style="display:flex;color:#333;"> <div style="margin-right:20px;">用户:{{ user }}</div> <div>园区:{{ garden }}</div> </div> <table border="1" cellspacing="0" cellpadding="3mm" width="1000px" style="margin-top:20px;color:pink;" > <tr> <td>日期</td> <td>姓名</td> <td>地址</td> </tr> <tr v-for="(item, index) in tableData" :key="index"> <td>{{ item.date }}</td> <td>{{ item.name }}</td> <td>{{ item.address }}</td> </tr> </table> <div id="main" style="width: 600px;height:400px;margin-top:50px;"></div> <img src="../../assets/bg-b.png" width="500px" style="margin-top:50px;" /> </div>
<el-button type="success" @click="printBox">打印Box</el-button>
必要的就是printable和type
printBox() {
setTimeout(function() {
print({
printable: 'printBox', //打印区域id
type: 'html', //打印类型是html
scanStyles: false,
targetStyles: ['*'],
})
}, 500)
},
<div style="display:flex;color:#333;">
scanStyles: false,
targetStyles: ['*'],
<template> <div class="test"> <div id="printBox"> <h3>打印预览</h3> <div style="display:flex;color:#333;"> <div style="margin-right:20px;">用户:{{ user }}</div> <div>园区:{{ garden }}</div> <div style="padding:30px;width:100px;height:50px;background:skyblue;border:1px solid #ccc;">测试padding</div> </div> <table border="1" cellspacing="0" cellpadding="3mm" width="1000px" style="margin-top:20px;color:pink;" > <tr> <td>日期</td> <td>姓名</td> <td>地址</td> </tr> <tr v-for="(item, index) in tableData" :key="index"> <td>{{ item.date }}</td> <td>{{ item.name }}</td> <td>{{ item.address }}</td> </tr> </table> <div id="main" style="width: 600px;height:400px;margin-top:20px;"></div> <img src="../../assets/bg-b.png" width="500px" style="margin-top:50px;" /> </div> <div class="btns"> <!-- 按钮 --> <el-button type="primary" @click="printAll">js打印</el-button> <el-button type="success" @click="printBox">打印Box</el-button> </div> </div> </template> <script> import print from 'print-js' import * as echarts from 'echarts' export default { data() { return { tableData: [ { date: '2016-05-02', name: '王小虎', address: '上海市普陀区金沙江路 1518 弄', }, { date: '2016-05-04', name: '王小虎', address: '上海市普陀区金沙江路 1517 弄', }, { date: '2016-05-01', name: '王小虎', address: '上海市普陀区金沙江路 1519 弄', }, { date: '2016-05-03', name: '王小虎', address: '上海市普陀区金沙江路 1516 弄', }, ], garden: '院士智谷', user: 'admin', } }, mounted() { this.getEchart() }, methods: { //原生的打印 printAll() { window.print() }, printBox() { setTimeout(function() { print({ printable: 'printBox', type: 'html', scanStyles: false, targetStyles: ['*'], // style: '#printBox{ display: block !important}', // 表格样式 // HonorMarginPadding: false, }) }, 500) }, getEchart() { // 基于准备好的dom,初始化echarts实例 const myChart = echarts.init(document.getElementById('main')) // 指定图表的配置项和数据 const option = { title: { text: 'ECharts 入门示例', }, tooltip: {}, legend: { data: ['销量'], }, xAxis: { data: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'], }, yAxis: {}, series: [ { name: '销量', type: 'bar', data: [5, 20, 36, 10, 10, 20], }, ], } // 使用刚指定的配置项和数据显示图表。 myChart.setOption(option) }, }, } </script> <style lang="scss" scoped> .test { margin: 50px; } </style>
浏览器效果
原生js打印
print打印
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。