vue二维码周围加文字。并实现打印功能。可运行Demo

安装两个插件:一个是生成二维码的,一个是打印的插件

npm install qrcodejs2 --save; 
npm install vue-print-nb;

Vue全局引用打印插件,方便使用。:

import Print from 'vue-print-nb' 
Vue.use(Print)

vue页面需要引入生成二维码插件:

import QRCode from 'qrcodejs2'

Vue页面的template代码:

<div>
      <div id="printTest">
        <div id="SetQRCode" style="page-break-after:always"></div>
        <div id="SetQRCode2" style="page-break-after:always"></div>
        <div id="SetQRCode3" style="page-break-after:always"></div>
      </div> <button v-print="'#printTest'">打印</button>
    </div>

js代码:

import QRCode from 'qrcodejs2'

/* vue生命周期函数*/
 mounted() {
    this.$nextTick(function () {
      this.testQRCode();
    })

  },
methods: {
    /* documentName绑定的是template中div的id,size是二维码的大小,qrText是扫描出来的文字,obj是需要传递的对象 */
    canvasEWM(documentName, size, qrText, obj) {
     
      // let qrcodeDiv = document.getElementById(documentName)
      let qrcode = new QRCode(document.getElementById(documentName), {
        text: qrText,
        width: size,
        height: size,
        colorDark: "#000",
        //容错率,L/M/H L最高
        correctLevel: QRCode.CorrectLevel.L 
      });

      var canvas = qrcode._el.firstChild;
      var ctx = canvas.getContext("2d");
      // 字体 粗体 格式
      const fontWeight = 'bold' 
      // 字体大小
      const fontSize = 14 
      // 底部文字上下间距
      const tb = 5 
      /* 右侧扩容宽度 */
      const addWidth = 200;
      // 文字行高
      const wenzilieheight = fontSize + 2 * tb;
      //实际高度 二维码的高度 200 +标签高度+ERP码的高度
      const realHeight = size + (fontSize + 2 * tb) * 2
      const realWidth = size + fontSize + 2 * tb + addWidth //实际宽度

      /* =============================设置二维码数据 =============================*/
      // 获取二维码数据
      const data = ctx.getImageData(0, 0, size, size);
      ctx.fillStyle = "#fff"
      canvas.setAttribute('height', realHeight); // 重设画布像素高度
      canvas.style.setProperty('height', realHeight + 'px'); // 重设画布实际高度
      canvas.setAttribute('width', realWidth); // 重设画布像素宽度
      canvas.style.setProperty('width', realWidth + 'px'); // 重设画布实际宽度
      ctx.fillRect(wenzilieheight, wenzilieheight, size, size)
      ctx.putImageData(data, wenzilieheight, wenzilieheight)// 填充二维码数据
      ctx.font = `${fontWeight} ${fontSize}px Arial`;
      //文字真实宽度
      const textWidth = ctx.measureText("山东冶金科学院物料标签").width;
      /* =============================设置标签 =============================*/
      // 设置底部背景色
      ctx.fillStyle = "#fff";
      /* 设置底部填充位置 */
      ctx.fillRect(0, 0, realWidth, wenzilieheight);
      ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
      ctx.fillStyle = "#333";// 字体颜色
      ctx.fillText(qrText, (size - textWidth) / 2, 0)
      /* =============================流水码 =============================*/
      // 根据字体大小计算文字left
      const fleft = (size - textWidth) / 2;
      const ftop = size + wenzilieheight + tb; //文字距顶部位置
      const textPadding = fontSize / 2; //字体边距为字体大小的一半可以自己设置
      // 设置底部背景色
      ctx.fillStyle = "#fff";
      /* 设置底部填充位置 */
      ctx.fillRect(wenzilieheight, size + wenzilieheight, size, realHeight - 2 * tb);
      // 设置字体填充位置
      ctx.fillRect(
        fleft,
        ftop,
        textWidth + textPadding,
        fontSize + textPadding
      );
      ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
      ctx.fillStyle = "#333";// 字体颜色
      ctx.fillText("ERP流水码", fleft, ftop)

      /* =============================设置右侧数据============================= */
      // 设置底部背景色
      ctx.fillStyle = "#fff";
      /* 设置右侧填充位置 */
      let rightX = size + wenzilieheight * 2;
      let rightY = 40;
      /*========名称小件======= */
      ctx.fillRect(rightX, rightY, size, realHeight - 2 * tb);
      // 设置字体填充位置
      ctx.fillRect(
        rightX,
        rightY,
        textWidth + textPadding,
        fontSize + textPadding
      );
      ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
      ctx.fillStyle = "#333";// 字体颜色
      ctx.fillText("名称:" + obj.materialName, rightX, rightY)
      /*========规格小件 绘制小件的时候每次都向下移动40个======= */
      // 设置底部背景色
      ctx.fillStyle = "#fff";
      rightY += 40
      ctx.fillRect(rightX, rightY, size, realHeight - 2 * tb);
      // 设置字体填充位置
      ctx.fillRect(
        rightX,
        rightY,
        textWidth + textPadding,
        fontSize + textPadding
      );
      ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
      ctx.fillStyle = "#333";// 字体颜色
      ctx.fillText("规格:" + obj.specification, rightX, rightY)
      /*========成分小件======= */
      // 设置底部背景色
      ctx.fillStyle = "#fff";
      rightY += 40
      ctx.fillRect(rightX, rightY, size, realHeight - 2 * tb);
      // 设置字体填充位置
      ctx.fillRect(
        rightX,
        rightY,
        textWidth + textPadding,
        fontSize + textPadding
      );
      ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
      ctx.fillStyle = "#333";// 字体颜色
      ctx.fillText("成分:" + obj.composition, rightX, rightY)
      /*========国别小件======= */
      // 设置底部背景色
      ctx.fillStyle = "#fff";
      rightY += 40
      ctx.fillRect(rightX, rightY, size, realHeight - 2 * tb);
      // 设置字体填充位置
      ctx.fillRect(
        rightX,
        rightY,
        textWidth + textPadding,
        fontSize + textPadding
      );
      ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
      ctx.fillStyle = "#333";// 字体颜色
      ctx.fillText("国别:" + obj.country, rightX, rightY)
      /*========状态小件======= */
      // 设置底部背景色
      ctx.fillStyle = "#fff";
      rightY += 40
      ctx.fillRect(rightX, rightY, size, realHeight - 2 * tb);
      // 设置字体填充位置
      ctx.fillRect(
        rightX,
        rightY,
        textWidth + textPadding,
        fontSize + textPadding
      );
      ctx.textBaseline = "top"; //设置绘制文本时的文本基线。
      ctx.fillStyle = "#333";// 字体颜色
      ctx.fillText("状态:" + obj.status, rightX, rightY)


    },
    testQRCode() {
      this.canvasEWM("SetQRCode", 200, "物料标签1", { composition: "123", materialName: "123", specification: "15*12*13", country: "中国", "status": "正常" });
      this.canvasEWM("SetQRCode2", 200, "物料标签12", { composition: "456", materialName: "456", specification: "15*12*13", country: "美国", "status": "正常" });
      this.canvasEWM("SetQRCode3", 200, "物料标签3", { composition: "789", materialName: "789", specification: "15*12*13", country: "澳大利亚", "status": "不正常" });
    },
}

要在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> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值