赞
踩
- //安装print-js
- npm install print-js --save
- 或
- yarn add print-js --save
!!!main.js全局引入或者单页面局部引入:
import printJS from 'print-js'
- <div style="width: 700px; padding-top: 22px;" id='printBill'>
- .....
- </div>
- mounted() {
- window.addEventListener('beforeprint', () => {
- // 打印开始时触发
- this.isLoading = this.$loading({
- lock: true,
- text: '加载中',
- spinner: 'el-icon-loading',
- background: 'rgba(0, 0, 0, 0.7)'
- });
- //打印开始时隐藏不必要显示的样式
- // this.isBorder = 'none'
- })
- window.addEventListener('afterprint', () => {
- // 打印结束是触发
- this.isLoading.close();
- //打印结束恢复必要显示的样式
- // this.isBorder = '1px solid #000000'
- })
- },
- methods: {
- //原生的打印
- printAll() {
- // const printHTML = document.querySelector('#printBill').innerHTML
- // // 将打印的区域赋值,进行打印 这种会出现多余的空白页
- // window.document.body.innerHTML = printHTML
- // window.print() // 调用window打印方法
- // window.location.reload() // 打印完成后重新加载页面
- const printHTML = document.querySelector('#printBill')
- window.document.body.innerHTML = "";
- // 将打印的区域赋值,进行打印 解决上面赋值方法
- window.document.body.appendChild(printHTML);
- window.print() // 调用window打印方法
- window.location.reload() // 打印完成后重新加载页面
- },
- }
- <style lang="scss" scoped>
- ::v-deep .el-textarea__inner {
- border: none;
- }
-
- @media print {
- @page {
- //landscape横向,portrait;纵向,如果不设置,则页面有横向和纵向的选择框
- // size: A4 portrait;
- //国家标准公文页边距 GB/T 9704-2012
- // margin: 3.7cm 2.6cm 3.5cm;
- margin: 4mm 10mm;
- }
- }
- </style>
- //打印
- billPrintClick() {
- setTimeout(function() {
- const style = '@page {margin:0 10mm};' //打印时去掉眉页眉尾
- printJS({
- printable: 'printBill', // 标签元素id
- type: 'html',
- header: '',
- targetStyles: ['*'],
- scanStyles: true, //打印必须加上,不然页面上的css样式无效
- style
- });
- }, 100)
- },
- <template>
- <div style="min-height: 720px;" class="column center">
- <div style="width: 700px; padding-top: 22px;" id='printBill'>
- <div style="font-size: 13px;color: #000000;">
- <div style="width: 100%;text-align: center;margin-top: 30px;font-size: 20px;">{{title}}</div>
- <div style="margin-top: 12px; width: 100%;" class="row flex-end">
- <div class="row">({{year}}):<span style="color: red;font-weight: 600;letter-spacing: 3px;">{{id}}</span>
- </div>
- </div>
- <div style="margin-top: 10px;" class="row space-between">
- <div class="flex1">
- 付款单位:<input v-model="PayerUnit" placeholder=""
- style="border: none;width: 140px;background-color: #fff;line-height: 25px;padding-left: 5px;"></input>
- </div>
- <div class="flex1">
- <input v-model="year" placeholder=""
- style="border: none;width: 50px;background-color: #fff;line-height: 25px;text-align: center;"></input>
- <span>年</span>
- <input v-model="month" placeholder=""
- style="border: none;width: 50px;background-color: #fff;line-height: 25px;text-align: center;"></input>
- <span>月</span>
- <input v-model="day" placeholder=""
- style="border: none;width: 50px;background-color: #fff;line-height: 25px;text-align: center;"></input>
- <span>日</span>
- </div>
- <div class="flex1 row flex-end">支票号:<input v-model="ChequeNumber" placeholder=""
- style="border: none;width: 140px;background-color: #fff;line-height: 25px;padding-left: 5px;"></input>
- </div>
- </div>
- <div style="margin-top:10px;line-height: 50px;">
- <table align="center" style="width: 100%;border-collapse: collapse;">
- <tr>
- <th style="width: 150px; letter-spacing: 20px;" :style="{border:isBorder}" align="center" valign="middle">
- 项目</th>
- <th style="width: 360px; letter-spacing: 20px;" :style="{border:isBorder}" align="center" valign="middle">
- 内容</th>
- <th style="width: 190px; letter-spacing: 20px;" :style="{border:isBorder}" align="center" valign="middle">
- 金额</th>
- </tr>
- <tr style="line-height: 150px;">
- <td style="" :style="{border:isBorder}" align="center" valign="middle">
- <div class="row center">
- <el-input type="textarea" :rows="10" placeholder="" v-model="project" resize='none'
- style="border: none;"></el-input>
- </div>
- </td>
- <td style="" :style="{border:isBorder}" align="center" valign="middle">
- <div class="row center">
- <el-input type="textarea" :rows="10" placeholder="" v-model="content" resize='none'
- style="border: none;"></el-input>
- </div>
- </td>
- <td style="" :style="{border:isBorder}" align="center" valign="middle">
- <div class="row center">
- <el-input type="textarea" :rows="10" placeholder="" v-model="money" resize='none'
- style="border: none;"></el-input>
- </div>
- </td>
- </tr>
- <tr style="" :style="{border:isBorder}">
- <td align="center" valign="middle">
- <div>
- 金额合计(大写):
- </div>
- </td>
- <td align="center" valign="middle" style="padding-right: 10px;">
- <input v-model="sum" placeholder="请输入金额合计(大写)"
- style="border: none;width: 100%;background-color: #fff;font-size: 18px;line-height: 40px;padding-left: 5px;"></input>
- </td>
- <td style="padding: 0 10px 0 5px;" :style="{border:isBorder}" align="center" valign="middle">
- <input v-model="sumNumber" placeholder=""
- style="border: none;width: 100%;background-color: #fff;font-size: 18px;line-height: 40px;padding-left: 5px;text-align: center;"></input>
- </td>
- </tr>
- </table>
- </div>
- <div style="margin-top: 10px;" class="row space-between">
- <div class="flex1">收款单位(盖章):</div>
- <div class="flex1">收款人:<input v-model="payee" placeholder=""
- style="border: none;width: 140px;background-color: #fff;line-height: 25px;padding-left: 5px;"></input>
- </div>
- <div class="flex1 row flex-end">交款人:<input v-model="Payer" placeholder=""
- style="border: none;width: 120px;background-color: #fff;line-height: 25px;padding-left: 5px;"></input>
- </div>
- </div>
- <div style="margin: 10px 0 0" class="row flex-end">
- 打印时间:2023-05-24 16:45
- </div>
- </div>
- </div>
- <div class="row flex-end" style="padding-right: 100px;margin-top: 20px;">
- <el-button type="primary" @click="printAll">js原生打印</el-button>
- <el-button type="success" @click="billPrintClick">printJS打印</el-button>
- </div>
- </div>
- </template>
-
- <script>
- import printJS from 'print-js'
- export default {
- data() {
- return {
- isBorder:'1px solid #000000',
- isLoading: null,
- title: '*******资金往来结算票据',
- year: '2023',
- month: '1',
- day: '1',
- id: '211000101251',
- ChequeNumber: '454454548212655995',
- sum: '壹佰捌拾圆整',
- sumNumber: '180.00',
- PayerUnit: '单位名称',
- payee: '章小鱼',
- Payer: '章小鱼',
- project: '餐补、住房补贴、出行补贴',
- content: '早餐、午餐、晚餐、住房补贴、出行补贴',
- money: '60.00/天、70.00/天、50.00/天',
- }
- },
- mounted() {
- window.addEventListener('beforeprint', () => {
- // 打印开始时触发
- this.isLoading = this.$loading({
- lock: true,
- text: '加载中',
- spinner: 'el-icon-loading',
- background: 'rgba(0, 0, 0, 0.7)'
- });
- //打印开始时隐藏不必要显示的样式
- // this.isBorder = 'none'
- })
- window.addEventListener('afterprint', () => {
- // 打印结束是触发
- this.isLoading.close();
- //打印结束恢复必要显示的样式
- // this.isBorder = '1px solid #000000'
- })
- },
- methods: {
- //原生的打印
- printAll() {
- // const printHTML = document.querySelector('#printBill').innerHTML
- // // 将打印的区域赋值,进行打印 这种会出现多余的空白页
- // window.document.body.innerHTML = printHTML
- // window.print() // 调用window打印方法
- // window.location.reload() // 打印完成后重新加载页面
- const printHTML = document.querySelector('#printBill')
- window.document.body.innerHTML = "";
- // 将打印的区域赋值,进行打印 解决上面赋值方法
- window.document.body.appendChild(printHTML);
- window.print() // 调用window打印方法
- window.location.reload() // 打印完成后重新加载页面
- },
- //打印
- billPrintClick() {
- setTimeout(function() {
- const style = '@page {margin:0 10mm};' //打印时去掉眉页眉尾
- printJS({
- printable: 'printBill', // 标签元素id
- type: 'html',
- header: '',
- targetStyles: ['*'],
- scanStyles: true, //打印必须加上,不然页面上的css样式无效
- style
- });
- }, 100)
- },
- }
- }
- </script>
-
- <style lang="scss" scoped>
- ::v-deep .el-textarea__inner {
- border: none;
- }
-
- @media print {
- @page {
- //landscape横向,portrait;纵向,如果不设置,则页面有横向和纵向的选择框
- // size: A4 portrait;
- //国家标准公文页边距 GB/T 9704-2012
- // margin: 3.7cm 2.6cm 3.5cm;
- margin: 4mm 10mm;
- }
- }
- </style>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。