当前位置:首页 > 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中实现收起展开面板功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-show或v-if控制显示隐藏 利用Vue的指令可以轻松实现面板的展开和收起:…

vue 实现pc

vue 实现pc

Vue 实现 PC 端应用的核心要点 Vue.js 是一个渐进式框架,适合构建 PC 端单页应用(SPA)或复杂的前端界面。以下是实现 PC 端应用的关键方法和注意事项: 项目初始化与工程化配置 使…

vue插件实现

vue插件实现

Vue 插件实现方法 Vue 插件是一种向 Vue 应用程序添加全局功能的机制。插件可以包含全局指令、过滤器、混入、实例方法等。 插件的基本结构 一个 Vue 插件通常是一个对象或函数,需要暴露一个…

vue实现换肤

vue实现换肤

使用 CSS 变量实现换肤 在 Vue 项目中,可以通过 CSS 变量(Custom Properties)动态切换主题色。CSS 变量可以在运行时修改,适合实现换肤功能。 定义全局 CSS 变量:…

vue实现抖动

vue实现抖动

Vue 实现抖动效果 在 Vue 中实现抖动效果可以通过 CSS 动画或 JavaScript 动画库来实现。以下是几种常见的方法: 使用 CSS 动画 通过定义 @keyframes 动画并在 V…

vue 实现滑动

vue 实现滑动

Vue 实现滑动效果的方法 在Vue中实现滑动效果可以通过多种方式,以下是几种常见的方法: 使用CSS过渡和动画 通过Vue的<transition>组件结合CSS过渡或动画实现滑动效果…