vue生成条形码和二维码并打印


前言

最近有一个需求,需要将产品信息生成标签,每个信息生成一个条形码,拿到所有数据生成二维码,最后打印标签。


一、生成条形码

使用jsbarcode,直接install,然后写这么一个组件,直接在页面引入,传入参数和内容即可,方便拓展和后期使用

<template>
    <svg :width="width" :height="height" :fontSize="fontSize" :displayValue="displayValue" :margin="margin" ref="barcode"></svg>
</template>
  
  <script>
  import JsBarcode from 'jsbarcode'
  
  export default {
    props: {
        value: {
            type: String,
            required: true
        },
        width: {
            type: Number,
            default: 2
        },
        height: {
            type: Number,
            default: 20
        },
        fontSize:{
            type: Number,
            default: 10
        },
        margin:{
            type: Number,
            default: 0
        },
        displayValue:{
            type: Boolean,
            default: true
        }
    },
    mounted() {
      JsBarcode(this.$refs.barcode, this.value, {
        width: this.width,
        height: this.height,
        fontSize:this.fontSize,
        displayValue:this.displayValue,
        margin:this.margin
      })
    }
  }
  </script>

二、生成二维码

同样是安装依赖直接写组件:

<template>
  <canvas  :width="width" :height="height" :fontSize="fontSize" ref="qrcode"></canvas>
</template>
  
  <script>
  import QRCode from 'qrcode';
  
  export default {
    name: 'QRCodeGenerator',
    props: {
      width: {
        type: Number,
        default: 20
      },
      height: {
        type: Number,
        default: 20
      },
      fontSize: {
        type: Number,
        default: 10
      },
      content: {
        type: String,
        required: true
      }
    },
    mounted() {
      this.generateQRCode();
    },
    methods: {
      generateQRCode() {
        const canvas = this.$refs.qrcode;
        const ctx = canvas.getContext('2d');
  
        // Set canvas size
        canvas.width = this.width;
        canvas.height = this.height;
  
        // Generate QR Code
        QRCode.toDataURL(this.content, {
          margin: 1,
          width: this.width,
          height: this.height,
          errorCorrectionLevel: 'H'
        }).then((url) => {
          // Draw QR Code on canvas
          const img = new Image();
          img.src = url;
          img.onload = () => {
            ctx.drawImage(img, 0, 0, this.width, this.height);
  
            // Add text below QR Code
            ctx.fillStyle = '#000000';
            ctx.font = `16px ${this.font}`;
            ctx.textAlign = 'center';
            ctx.fillText(this.content, this.width / 2, this.height + 20);
          };
        });
      }
    }
  };
  </script>

三、效果图

在标签中使用组件生成标签,截图部分
在这里插入图片描述

四、打印

1、直接安装依赖,使用vue-print-nb

2、全局引入使用

import Print from "vue-print-nb";

Vue.use(Print);

3、在打印的内容中添加一个id

<el-table id="printBox"> ......

4、添加按钮绑定v-print

 <el-button
  	size="small"
    type="success"
    icon="el-icon-printer"
    style="margin-right:40px;float:right;"
    v-print="'#printBox'"
    @click="printTag"
  >
      打印
  </el-button>

5、点击按钮打印就能出现打印预览页面啦,就可以去配置打印机打印啦…


  • 4
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要在Vue生成二维码并打印,你可以使用qrcode.js和html2canvas库。下面是一个简单的实现示例: 1. 安装qrcode.js和html2canvas ```bash npm install qrcodejs2 html2canvas --save ``` 2. 在Vue组件中引入库 ```javascript import QRCode from 'qrcodejs2' import html2canvas from 'html2canvas' ``` 3. 在Vue组件中定义一个函数来生成二维码 ```javascript generateQRCode() { const qrcode = new QRCode('qrcode', { text: 'https://www.example.com', // 二维码内容 width: 256, // 二维码宽度 height: 256, // 二维码高度 colorDark : '#000000', // 二维码颜色 colorLight : '#ffffff', // 二维码背景色 correctLevel : QRCode.CorrectLevel.H // 二维码纠错级别 }) } ``` 4. 在Vue组件中定义一个函数来打印页面 ```javascript print() { html2canvas(document.body).then(canvas => { const printWindow = window.open('', 'Print', 'height=600,width=800') printWindow.document.write('<html><head><title>Print</title></head><body></body></html>') printWindow.document.body.appendChild(canvas) printWindow.document.close() printWindow.print() }) } ``` 5. 在Vue组件中使用生成二维码打印页面的函数 ```vue <template> <div> <div id="qrcode"></div> <button @click="generateQRCode">生成二维码</button> <button @click="print">打印页面</button> </div> </template> <script> import QRCode from 'qrcodejs2' import html2canvas from 'html2canvas' export default { methods: { generateQRCode() { const qrcode = new QRCode('qrcode', { text: 'https://www.example.com', // 二维码内容 width: 256, // 二维码宽度 height: 256, // 二维码高度 colorDark : '#000000', // 二维码颜色 colorLight : '#ffffff', // 二维码背景色 correctLevel : QRCode.CorrectLevel.H // 二维码纠错级别 }) }, print() { html2canvas(document.body).then(canvas => { const printWindow = window.open('', 'Print', 'height=600,width=800') printWindow.document.write('<html><head><title>Print</title></head><body></body></html>') printWindow.document.body.appendChild(canvas) printWindow.document.close() printWindow.print() }) } } } </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值