1. 程式人生 > 其它 >vue列印外掛vue-print-nb的實現

vue列印外掛vue-print-nb的實現

1.引入外掛  npm install vue-print-nb --save

在main.js中引入  

import Print from 'vue-print-nb' Vue.use(Print) 

2.html程式碼

<div class="box">
    <div id="printTest" class="upTable">
      <table>
        <tr>
          <td class="title" colspan="4">木材檢尺報告書</td>
        </
tr> <tr> <td class="one">船名</td> <td style="width:190px">{{ goods.shipName }}</td> <td class="one">輸出國</td> <td>{{ goods.exportCountry }}</td> </tr> <tr> <td class
="one">樹種</td> <td>{{ variety }}</td> <td class="one">委託方/貨主</td> <td>{{ goods.goodsMaster }}</td> </tr> <tr> <td class="one">申報材積</td> <td>{{ goods.declareWoodVolume }}立方米</
td> <td class="one">申報數量</td> <td>{{ goods.declareNumber }}根</td> </tr> <tr> <td class="one">存放地點</td> <td>{{ goods.goodsYard }}</td> <td class="one">卸畢時間</td> <td v-if="goods.unloadTime">{{ goods.unloadTime.substring(0,10) }}</td> <td v-else /> </tr> <tr> <td class="one">檢驗標準</td> <td colspan="3">GB/T 144-2013 國家標準</td> </tr> <tr> <td class="title2" colspan="4">檢驗結果</td> </tr> </table> <table class="dataTable"> <tr> <td style="width:210px">垛位號</td> <td style="width:100px">長度</td> <td style="width:100px">已檢整木</td> <td style="width:100px">材積</td> <td style="width:100px">斷木</td> <td style="width:0">未檢整木</td> </tr> <tr v-for="(item,index) in shortData" :key="index"> <td>{{ item.placeNumber }}</td> <td>{{ item.placeLength }}</td> <td>{{ item.zs }}</td> <td>{{ item.woodVolume }}</td> <td>{{ item.damagedWood }}</td> <td>{{ item.notCheckWood }}</td> </tr> <tr> <td style="width:210px">合計</td> <td style="width:100px" /> <td style="width:100px">{{ zsAll }}</td> <td style="width:100px">{{ woodVolumeAll }}</td> <td style="width:100px">{{ damagedWoodAll }}</td> <td style="width:0px">{{ notCheckWoodAll }}</td> </tr> </table> </div> <el-button v-print="'#printTest'" type="primary" style="margin-top:20px"> 列印 </el-button> </div>

3.js程式碼

<script>
export default {
  props: ['catList', 'goods'],
  data() {
    return {
    //和下邊 <style media="printTest"> 一起的作用是去掉頁首頁尾、去掉多出空白頁的問題
      printObj: {
        id: 'printTest',
        popTitle: '',
        ectraHead: ''
      },
      shortData: [],
      variety: '',
      zsAll: 0, // 已檢整木  總數
      woodVolumeAll: 0, // 材積
      damagedWoodAll: 0, // 斷木
      notCheckWoodAll: 0, // 未檢整木
      updateTime: ''
    }
  },
  methods: {
  //這裡的資料是在父頁面傳來的
    getvariety(variety, catList, goods) {
      this.variety = variety
      this.shortData = catListthis.goods = goods// 合計
      let zsAll = 0
      let woodVolumeAll = 0
      let damagedWoodAll = 0
      let notCheckWoodAll = 0
      this.shortData.map((item) => {
        zsAll += item.zs
        woodVolumeAll += item.woodVolume
        damagedWoodAll += item.damagedWood
        notCheckWoodAll += item.notCheckWood
      })
      this.zsAll = zsAll
      this.woodVolumeAll = woodVolumeAll
      this.damagedWoodAll = damagedWoodAll
      this.notCheckWoodAll = notCheckWoodAll
    },
  }
}
</script>

4.樣式

<style media="printTest">
@page {
    size: auto;
    margin: 3mm;
  }

  html {
    background-color: #ffffff;
    height: auto;
    margin: 0px;
  }

  body {
    border: solid 1px #ffffff;
    margin: 10mm 15mm 10mm 15mm;
  }
</style>

<style lang="less" scoped>
.upTable{
    width: 100%;
    height: 50%;
    margin-top: 10px;
    table{
        width: 100%;
        border-collapse:collapse
    }
    td{
        border: 1px solid #000;
        font-size: 18px;
        text-align: center;
        font-family: Source Han Sans CN;
        font-weight: 450;
        color: #000000;
    }
    .title{
        font-size: 20px;
        text-align: center;
        height: 50px;
        font-family: Source Han Sans CN;
        font-weight: 550;
        color: #000000;
    }
    .one{
        width: 20%;
        height: 40px;
        font-size: 18px;
        text-align: center;
        font-family: Source Han Sans CN;
        font-weight: 550;
        color: #000000;
    }
    .title2{
        font-size: 18px;
        text-align: center;
        height: 45px;
        font-family: Source Han Sans CN;
        font-weight: 550;
        color: #000000;
    }
    .dataTable{
        border-top: 0px solid #000000;
        td{
            //  border: 1px solid #000;
            font-size: 18px;
            text-align: center;
            font-family: Source Han Sans CN;
            font-weight: 450;
            color: #000000;
            padding: 5px 0;
        }
    }
}
.el-button{
    margin-right: 20px;
    margin-left: 20px;
    width: 100px;
    padding: 12px 0;
}