printDialog.vue 2.57 KB
<template>
  <el-dialog
    title="任务打印"
    :visible.sync="visible"
    :showprint="showprint"
    width="800px"
    append-to-body
    @close="$emit('update:showprint', false)"
  >
    <div ref="print">
      <el-row type="flex" class="row-bg" justify="space-between">
        <el-col :span="11"
          ><div class="date">{{ parseTime(printDate) }}</div></el-col
        >
        <el-col :span="13"><div class="ad-sheet">拣货明细</div></el-col>
      </el-row>
      <el-row :class="{hide: isHide}">
        <el-button
          size="mini"
          type="primary"
          icon="el-icon-printer"
          @click="print"
          class="pull-right"
          >打印</el-button
        >
      </el-row>

      <el-divider></el-divider>
      <el-table :data="detailList">
        <el-table-column label="库位" align="center" prop="companyCode" />
        <el-table-column label="容器" align="center" prop="locationCode" />
        <el-table-column label="物料编码" align="center" prop="materialCode" />
        <el-table-column label="物料名称" align="center" prop="materialName" />
        <el-table-column label="数量" align="center" prop="qty" />
        <el-table-column label="条码" align="center" prop="materialUnit" />
      </el-table>
    </div>
  </el-dialog>
</template>

<script>
import html2canvas from "html2canvas";
export default {
  props: {
    showprint: {
      type: Boolean,
      default: false,
    },
    printlist: {
      type: Array,
    },
  },
  data() {
    return {
      printDate: new Date(),
      visible: this.showprint,
      detailList: this.printlist,
      isHide:false,
    };
  },
  watch: {
    showprint() {
      this.visible = this.showprint;
    },
    printlist() {
      this.detailList = this.printlist;
    },
  },
  methods: {
    print() {
      this.isHide = true;
      // 转图片打印
      const timer = setTimeout(()=> {
        this.isHide = false;
        html2canvas(this.$refs.print, {
        allowTaint: false,
        taintTest: true,
        useCORS: true,
        scale: 1.5,
        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);
      });
      })
      this.$once('hook:beforeDestroy',()=>{
        clearInterval(timer);
        timer = null;
      })

    },
  },
};
</script>

<style lang="scss" scoped>
.no-print {
  margin-top: 15px;
}
.ad-sheet {
  font-size: 22px;
}
.hide {
  display: none;
}
</style>