printDialog.vue 2.75 KB
<template>
  <el-dialog
    title="打印"
    :visible.sync="visible"
    width="500px"
    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">
            <el-image>
              <div :id="'qrcode'+key" ref="qrcode"></div>
            </el-image>
          </th>
        </tr>
        <tr>
          <th>
            物料名称
          </th>
          <th>
            {{item.type}}
          </th>
        </tr>
        <tr>
          <th>
            物料规格
          </th>
          <th>
            {{item.spec}}
          </th>
        </tr>
      </table>
    </div>
  </el-dialog>
</template>

<script>
  import html2canvas from 'html2canvas'


  export default {
    name: 'printDialog',
    props: {
      tableData: {
        type:Array,
        defaults:[]
      },
      showP: {
        type: Boolean,
        defaults: false
      }
    },
    watch: {
      showP() {
        this.visible = this.showP
        this.qrcode()
      }
  },
  data(){
    return {
      visible: this.showP,
      gqrcode: ''
    }
  },
    mounted() {
      this.$nextTick(()=>console.log(this.$refs.qrcode))

    },
  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 {

  }
</style>