当前位置:首页 > VUE

vue实现标签打印

2026-02-18 07:09:09VUE

实现标签打印的Vue方案

在Vue中实现标签打印功能,通常需要结合浏览器打印API和CSS打印样式控制。以下是几种常见实现方式:

使用window.print()基础打印

创建打印专用组件,通过CSS控制打印样式:

vue实现标签打印

<template>
  <div>
    <button @click="print">打印标签</button>
    <div class="print-content" ref="printContent">
      <!-- 标签内容 -->
      <div class="label">产品标签示例</div>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    print() {
      const printWindow = window.open('', '_blank');
      printWindow.document.write(this.$refs.printContent.innerHTML);
      printWindow.document.close();
      printWindow.focus();
      printWindow.print();
      printWindow.close();
    }
  }
}
</script>

<style>
@media print {
  body * {
    visibility: hidden;
  }
  .print-content, .print-content * {
    visibility: visible;
  }
  .print-content {
    position: absolute;
    left: 0;
    top: 0;
  }
}
</style>

使用html2canvas+jsPDF实现PDF打印

对于需要精确控制打印布局的场景:

<template>
  <button @click="exportToPDF">导出PDF</button>
  <div id="labelContent">
    <!-- 标签内容 -->
  </div>
</template>

<script>
import html2canvas from 'html2canvas';
import jsPDF from 'jspdf';

export default {
  methods: {
    async exportToPDF() {
      const element = document.getElementById('labelContent');
      const canvas = await html2canvas(element, {
        scale: 2,
        logging: false,
        useCORS: true
      });

      const imgData = canvas.toDataURL('image/png');
      const pdf = new jsPDF({
        orientation: 'portrait',
        unit: 'mm'
      });

      const imgWidth = 50; // 标签宽度(mm)
      const imgHeight = 30; // 标签高度(mm)
      pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight);
      pdf.save('label.pdf');
    }
  }
}
</script>

使用专用打印插件

对于专业标签打印需求,可考虑专用库:

vue实现标签打印

npm install vue-thermal-print
<template>
  <button @click="printLabel">打印标签</button>
</template>

<script>
import { ThermalPrinter } from 'vue-thermal-print';

export default {
  methods: {
    printLabel() {
      const printer = new ThermalPrinter({
        type: 'epson',
        interface: 'USB',
        width: 80 // 标签宽度(mm)
      });

      printer.align('CT');
      printer.println('产品标签');
      printer.cut();
      printer.execute();
    }
  }
}
</script>

批量标签打印实现

对于需要打印多个标签的场景:

<template>
  <div>
    <button @click="printBatch">批量打印</button>
    <div class="labels-container" ref="labels">
      <div class="label" v-for="(item, index) in labels" :key="index">
        {{ item.name }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      labels: [{name: '标签1'}, {name: '标签2'}] // 标签数据
    }
  },
  methods: {
    printBatch() {
      const printContent = this.$refs.labels.innerHTML;
      const printWindow = window.open('', '_blank');
      printWindow.document.write(`
        <!DOCTYPE html>
        <html>
          <head>
            <style>
              @page {
                size: A4;
                margin: 0;
              }
              body {
                display: grid;
                grid-template-columns: repeat(3, 1fr);
                gap: 10mm;
                padding: 10mm;
              }
              .label {
                border: 1px dashed #ccc;
                padding: 5mm;
                height: 30mm;
              }
            </style>
          </head>
          <body>${printContent}</body>
        </html>
      `);
      printWindow.document.close();
      printWindow.print();
    }
  }
}
</script>

打印样式优化建议

针对标签打印的特殊需求,CSS媒体查询可以优化打印效果:

@media print {
  @page {
    size: 50mm 30mm; /* 自定义标签尺寸 */
    margin: 0;
  }
  body {
    margin: 0;
    padding: 0;
    -webkit-print-color-adjust: exact;
  }
  .no-print {
    display: none !important;
  }
  .page-break {
    page-break-after: always;
  }
}

以上方案可根据实际需求选择或组合使用,对于工业级标签打印,建议使用专业打印插件或与后端打印服务集成。

标签: 标签vue
分享给朋友:

相关文章

vue实现过程

vue实现过程

Vue 的实现过程 Vue 的实现过程可以分为多个核心模块,包括响应式系统、虚拟 DOM、模板编译等。以下是 Vue 的主要实现过程分析。 响应式系统 Vue 的响应式系统基于 Object.def…

vue实现bootstrap

vue实现bootstrap

Vue 中集成 Bootstrap 的方法 在 Vue 项目中集成 Bootstrap 可以通过以下方式实现,涵盖样式、组件和交互功能。 安装 Bootstrap 依赖 通过 npm 或 yarn…

computed vue 实现

computed vue 实现

computed 在 Vue 中的基本用法 Vue 的 computed 属性用于声明依赖于其他数据的计算属性。当依赖的数据变化时,计算属性会自动重新计算并缓存结果。 export defaul…

vue筛选实现

vue筛选实现

Vue 筛选实现方法 使用计算属性实现筛选 计算属性是 Vue 中实现数据筛选的常用方式。通过定义一个计算属性,可以根据输入的条件动态过滤数据。 <template> <di…

vue实现route

vue实现route

Vue 路由实现方法 Vue 中实现路由通常使用 vue-router 库,这是 Vue 官方推荐的路由管理器。以下是具体实现步骤: 安装 vue-router 通过 npm 或 yarn 安装…

vue实现图片

vue实现图片

Vue 实现图片功能的方法 静态图片引入 在 Vue 项目中,可以通过 src 属性直接引入静态图片。图片通常放在 public 或 assets 目录下。 <template>…