printDialog.vue 2.99 KB
<template>
  <el-dialog
    title="打印"
    :visible.sync="visible"
    width="1000px"
    append-to-body
    @close="$emit('update:showP', false)"
  >
    <el-button
      size="mini"
      type="primary"
      icon="el-icon-printer"
      @click="print"
      class="pull-right"
    >打印
    </el-button>

    <div ref="print">
      <table v-for="(item,key) in tableData"
             border="1"
             cellpadding="8"
             class="table"
      >
        <tr>
          <th>
            物料编码
          </th>
          <th colspan="2">
            {{item.code}}
          </th>
        </tr>
        <tr>
          <th>
            物料类别
          </th>
          <th>
            {{item.type}}
          </th>
          <th rowspan="3">
            <qrcode-vue :value="'物料编码:'+item.code+', 物料类别:'+item.type+', 物料名称:'+item.name+', 物料规格:'+item.spec"
                        size="60"
                        level="H"
                        renderAs="svg"></qrcode-vue>
          </th>
        </tr>
        <tr>
          <th>
            物料名称
          </th>
          <th>
            {{item.name}}
          </th>
        </tr>
        <tr>
          <th>
            物料规格
          </th>
          <th>
            {{item.spec}}
          </th>
        </tr>
      </table>
    </div>
  </el-dialog>
</template>

<script>
  import html2canvas from 'html2canvas'
  import QrcodeVue from 'qrcode.vue'

  export default {
    name: 'printDialog',
    components: {
      QrcodeVue,
    },
    props: {
      tableData: {
        type:Array,
        defaults:()=>[]
      },
      showP: {
        type: Boolean,
        defaults: false
      }
    },
    watch: {
      showP() {
        this.visible = this.showP
      }
  },
  data(){
    return {
      visible: this.showP,
      gqrcode: ''
    }
  },
  methods: {
    print()
    {
      const timer = setTimeout(() => {
        html2canvas(this.$refs.print, {
          allowTaint: false,
          taintTest: true,
          useCORS: true,
          windowHeight: document.body.scrollHeight,
          background: '#fff'
        }).then(canvas => {
          const url = canvas.toDataURL('image/jpeg', 1.0)
          const myimg = document.createElement('img')
          myimg.src = url
          this.$print(myimg)
        })
      })
    }
  ,
    // qrcode() {
    //   this.$nextTick(()=> {
    //     // console.log(this.$refs.qrcode)
    //     this.tableData.forEach((item, index) => {
    //       console.log(index)
    //       let qr = new QRCode('qrcode' + index, {
    //         width: 100,
    //         height: 100, // 高度
    //         render: 'canvas',
    //         text: item.code + '/' + item.name + '/' + item.spec,
    //         correctLevel: QRcode.CorrectLevel.L
    //       })
    //       console.log(1)
    //       console.log(qr)
    //     })
    //   })
    // }
  },

  }
</script>

<style scoped>
  .table {
    min-width:400px;
    position:relative;
    left:30%;
  }
</style>