vue 批量打印二维码含LOGO图片


前言

实现效果如下图↓

在这里插入图片描述


一、实现思路

  1. 通过qrcodejs2插件生成二维码
  2. 通过html2canvas插件对页面指定区域进行图片生成
  3. 通过print-js插件对生成的图片进行打印

二、下载相应库

import QRCode from 'qrcodejs2';
import html2canvas from "html2canvas"; //生成图片
import printJs from "print-js"; //文件压缩

三、封装代码

  • template

代码如下:

<template>
  <div class="print-box">
    <div ref="print" id="printBox">
      <div v-for="(item, index) in QRCodeList" :key="index" class="item" ref="html2canvasRef">
        <div :ref="id"></div>
        <h4 v-if="item.title" class="QRCode_title">{{ item.title }}</h4>
      </div>
    </div>
  </div>
</template>
  • style

代码如下:

<style lang="less">
.print-box {
  display: none;
}
</style>
  • js
<script>
import QRCode from 'qrcodejs2';
import html2canvas from "html2canvas"; //生成图片
import printJS from "print-js";

export default {
  data() {
    return {
      QRCodeList: [],
      html2canvas: null,
    }
  },
  props: {
    id: {
      type: String,
      default: "QRCode",
      required: true
    },
    width: {
      type: String | Number,
      default: 150
    },
    height: {
      type: String | Number,
      default: 150
    },
    colorDark: {
      type: String,
      default: '#000000'
    },
    colorLight: {
      type: String,
      default: '#ffffff'
    },
    logoUrl: {
      type: String,
      default: ''
    },
  },
  methods: {
    initQRCode(data = {}) {
      const index = data.index;
      const QRCodeRefs = this.$refs[this.id];
      return new Promise((resolve) => {
        const qrcode = new QRCode(QRCodeRefs[index], {
          text: data.id,
          width: this.width,
          height: this.height,
          colorDark: this.colorDark,
          colorLight: this.colorLight,
          correctLevel: QRCode.CorrectLevel.H,
          margin: 5,
          type: "image/png"
        });
        let canvas = qrcode._el.getElementsByTagName("canvas")[0];
        if (this.logoUrl) {
          let img = qrcode._el.getElementsByTagName("img")[0];
          let ctx = canvas.getContext("2d");

          let logo = new Image();
          logo.crossOrigin = "Anonymous";
          logo.src = this.logoUrl;

          logo.onload = () => {
            let codeWidth = (this.width * 0.8) / 2;
            let codeHeight = (this.height * 0.8) / 2;
            ctx.drawImage(logo, codeWidth, codeHeight, this.width * 0.2, this.height * 0.2);
            img.src = canvas.toDataURL();
            resolve(qrcode);
          }
        } else {
          resolve(qrcode);
        }
      })
    },
    initQRCodeList(QRCodeList = []) {
      this.QRCodeList = QRCodeList;
    },

    async onPrintQRCode() {
      const html2canvasRefs = this.$refs.html2canvasRef;
      for (let i = 0; i < this.QRCodeList.length; i++) {
        const item = this.QRCodeList[i];
        const QRCodeRefs = this.$refs[this.id];
        QRCodeRefs[i].innerHTML = '';
        await this.initQRCode({...item, index: i});
      }
      const style = `@media print {
        #printBox {
          display: flex;
          flex-wrap: wrap;
          align-items: flex-start;
          .item{
            flex: 0 0 24%;
            padding-right: calc(4% / 3);
            padding-bottom: calc(4% / 3);

            display: flex;
            flex-direction: column;
            align-items: center;
            &:nth-child(4n) {
              padding-right: 0;
            }
            &:last-child {
              padding-right: auto;
            }
            h4{
              padding: 15px 0;
              text-align: center;
            }
          }
        }
      }`

      setTimeout(() => {
        printJS({
          printable: "printBox",
          type: "html",
          maxWidth: "100%",
          scanStyles: false,
          style: style
        });
      })
    },
  },

}
</script>

四、效果

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

笑看、人世间@

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值