pengwei
2025-03-07 7cc3ed0f7aca03e39fa65d617f2a51ff531a12f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<template>
    <div id="goods-template" class="print-hide">
        <div v-for="i in goods" :key="i.id" style="width:370px;height: 200px; display: flex;">
            <div style="width: 250px;">
                <div style="line-height: 30px;">物料编号:{{ i.goodsCode }}</div>
                <div style="line-height: 30px;">物料名称:{{ i.goodsName }}</div>
                <div style="line-height: 30px;">物料型号:{{ i.goodsSpec }}</div>
                <div style="line-height: 30px;">物料来源:{{ i.goodsSource }}</div>
                <div style="line-height: 30px;">物料类型:{{ i.goodsType }}</div>
            </div>
            <div style="width:120px;">
                <qrcode-vue :size="120" :margin="0" :auto-color="true" :dot-scale="1" :text="i.qrCodeNo" />
                <div style="text-align: center;line-height: 40px;">{{ i.qrCodeNo }}</div>
            </div>
        </div>
    </div>
</template>
 
<script>
import printJS from "print-js";
import QrcodeVue from 'vue-qr/src/packages/vue-qr.vue'
 
export default {
    components: {
        QrcodeVue,
    },
    props: {
        goods: {
            type: Object,
            default: () => ([]),
        }
    },
    mounted() {
        setTimeout(() => {
            this.handlePrint();
        }, 500);
    },
    beforeUnmount() {
        clearTimeout()
    },
    methods: {
        handlePrint() {
            // 获取打印模板的DOM元素
            const printElement = document.getElementById("goods-template");
            console.log(printElement);
            if (printElement) {
                // 显示打印模板,以便能够打印
                printElement.classList.remove("print-hide");
                // 使用vue-printjs打印模板内容
                printJS({
                    printable: printElement,
                    type: "html",
                    scanStyles:false,
                    error: (err) => {
                        console.log("err", err);
                    },
                });
                // 打印完成后隐藏打印模板
                printElement.classList.add("print-hide");
                //向父组件发送打印完成事件
                this.$emit("print-complete");
            }
        }
    },
};
</script>
<style lang="less" scoped>
.print-hide {
    display: block;
    /* 在屏幕上隐藏打印模板 */
}
</style>