当前位置:首页 > VUE

vue实现打印二维码

2026-01-12 08:43:02VUE

安装依赖

需要安装 qrcodehtml2canvas 库来生成二维码并转换为可打印的图像。

npm install qrcode html2canvas

生成二维码

使用 qrcode 生成二维码并渲染到页面。

vue实现打印二维码

<template>
  <div>
    <canvas id="qrcode-canvas"></canvas>
    <button @click="printQRCode">打印二维码</button>
  </div>
</template>

<script>
import QRCode from 'qrcode';

export default {
  data() {
    return {
      qrData: 'https://example.com', // 二维码内容
    };
  },
  mounted() {
    this.generateQRCode();
  },
  methods: {
    generateQRCode() {
      const canvas = document.getElementById('qrcode-canvas');
      QRCode.toCanvas(canvas, this.qrData, { width: 200 }, (error) => {
        if (error) console.error(error);
      });
    },
  },
};
</script>

打印二维码

使用 html2canvas 将二维码转换为图像并调用浏览器打印功能。

vue实现打印二维码

<script>
import html2canvas from 'html2canvas';

export default {
  methods: {
    printQRCode() {
      const canvas = document.getElementById('qrcode-canvas');
      html2canvas(canvas).then((printCanvas) => {
        const printWindow = window.open('', '_blank');
        printWindow.document.write('<img src="' + printCanvas.toDataURL() + '"/>');
        printWindow.document.close();
        printWindow.onload = () => printWindow.print();
      });
    },
  },
};
</script>

优化打印样式

可以调整打印页面的样式,确保二维码居中且无额外边距。

<script>
methods: {
  printQRCode() {
    const canvas = document.getElementById('qrcode-canvas');
    html2canvas(canvas).then((printCanvas) => {
      const printWindow = window.open('', '_blank');
      printWindow.document.write(`
        <style>
          body { margin: 0; display: flex; justify-content: center; align-items: center; height: 100vh; }
          img { max-width: 100%; max-height: 100%; }
        </style>
        <img src="${printCanvas.toDataURL()}"/>
      `);
      printWindow.document.close();
      printWindow.onload = () => printWindow.print();
    });
  },
},
</script>

支持多二维码打印

若需批量打印多个二维码,可以通过循环生成并拼接图像。

<template>
  <div>
    <div v-for="(item, index) in qrList" :key="index">
      <canvas :id="'qrcode-canvas-' + index"></canvas>
    </div>
    <button @click="printAllQRCode">打印全部</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      qrList: ['https://example.com/1', 'https://example.com/2'],
    };
  },
  mounted() {
    this.qrList.forEach((data, index) => {
      const canvas = document.getElementById(`qrcode-canvas-${index}`);
      QRCode.toCanvas(canvas, data, { width: 200 });
    });
  },
  methods: {
    printAllQRCode() {
      const promises = this.qrList.map((_, index) => {
        const canvas = document.getElementById(`qrcode-canvas-${index}`);
        return html2canvas(canvas);
      });

      Promise.all(promises).then((canvases) => {
        const printWindow = window.open('', '_blank');
        let html = '<style>body { margin: 0; } img { margin: 10px; }</style>';
        canvases.forEach(canvas => {
          html += `<img src="${canvas.toDataURL()}"/>`;
        });
        printWindow.document.write(html);
        printWindow.document.close();
        printWindow.onload = () => printWindow.print();
      });
    },
  },
};
</script>

标签: 二维码vue
分享给朋友:

相关文章

项目基于vue实现

项目基于vue实现

基于Vue实现项目的关键方法 搭建基础环境 安装Node.js和npm/yarn,确保开发环境具备运行Vue的能力。通过Vue CLI快速初始化项目结构,选择所需配置(如Babel、Router、Vu…

vue 实现blog

vue 实现blog

Vue 实现博客的基本步骤 使用 Vue 实现博客可以分为前端和后端两部分,前端使用 Vue.js 框架,后端可以选择 Node.js、Python 或其他服务端语言。以下是一个基于 Vue 的博客实…

vue实现图集

vue实现图集

Vue 实现图集的方法 在 Vue 中实现图集功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用第三方库(如 vue-image-lightbox) 安装 vue-image-ligh…

vue实现签章

vue实现签章

Vue 实现签章功能 签章功能通常包括手写签名、电子印章等。以下是基于 Vue 的实现方法: 使用 canvas 实现手写签名 安装依赖(如需要): npm install signatu…

vue实现toast

vue实现toast

Vue 实现 Toast 的方法 使用第三方库(推荐) 对于快速实现 Toast 功能,推荐使用成熟的第三方库如 vue-toastification 或 vant 的 Toast 组件。 安装 v…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…