当前位置:首页 > VUE

vue实现打印标签

2026-03-09 11:41:43VUE

使用 Vue 实现打印标签

安装打印插件

推荐使用 vue-print-nb 插件实现打印功能,安装命令如下:

npm install vue-print-nb --save

注册打印指令

在 Vue 项目的入口文件(如 main.js)中注册打印指令:

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

创建打印区域模板

在需要打印的组件中,设置打印区域和触发按钮:

<template>
  <div>
    <div id="printArea">
      <!-- 这里是标签内容 -->
      <div v-for="(item, index) in labels" :key="index">
        <div class="label-item">{{ item.text }}</div>
      </div>
    </div>
    <button v-print="printConfig">打印标签</button>
  </div>
</template>

配置打印参数

在组件中配置打印参数:

vue实现打印标签

export default {
  data() {
    return {
      labels: [
        { text: '标签1' },
        { text: '标签2' },
        { text: '标签3' }
      ],
      printConfig: {
        id: 'printArea',
        popTitle: '标签打印',  // 打印标题
        extraCss: 'https://example.com/css/print.css',  // 额外CSS
        extraHead: '<meta http-equiv="Content-Language"content="zh-cn"/>'  // 额外头部信息
      }
    }
  }
}

样式优化

添加打印专用样式,确保打印效果符合预期:

@media print {
  body * {
    visibility: hidden;
  }
  #printArea, #printArea * {
    visibility: visible;
  }
  .label-item {
    page-break-inside: avoid;  /* 避免标签被分页截断 */
    margin: 10px;
    border: 1px dashed #ccc;
  }
}

动态生成标签内容

可以通过方法动态生成要打印的标签内容:

methods: {
  generateLabels() {
    this.labels = Array.from({length: 10}, (_, i) => ({
      text: `产品标签-${i+1}`,
      barcode: `CODE128-${Math.random().toString(36).substr(2,8)}`
    }))
  }
}

使用第三方库生成条形码

如需打印带条形码的标签,可安装 vue-barcode 组件:

vue实现打印标签

npm install @chenfengyuan/vue-barcode

在组件中使用:

<template>
  <div id="printArea">
    <div v-for="(item, index) in labels" :key="index">
      <vue-barcode :value="item.barcode" format="CODE128"></vue-barcode>
      <div>{{ item.text }}</div>
    </div>
  </div>
</template>

<script>
import VueBarcode from '@chenfengyuan/vue-barcode'
export default {
  components: { VueBarcode }
}
</script>

打印前的回调处理

可以添加打印前和打印后的回调函数:

printConfig: {
  id: 'printArea',
  beforeOpenCallback(vue) {
    console.log('打开打印预览前')
  },
  openCallback(vue) {
    console.log('打开打印预览后')
  },
  closeCallback(vue) {
    console.log('关闭打印预览后')
  }
}

批量打印控制

对于大批量标签打印,建议分页处理避免内存问题:

printBatchLabels() {
  const batchSize = 50
  for(let i=0; i<this.labels.length; i+=batchSize) {
    const batch = this.labels.slice(i, i+batchSize)
    this.printBatch(batch)
  }
}

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

相关文章

vue实现fadein

vue实现fadein

Vue 实现 FadeIn 效果 在 Vue 中实现淡入(FadeIn)效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法: 使用 CSS 过渡 通过 Vue 的过渡系统结合 CSS…

vue tab实现

vue tab实现

Vue Tab 实现方法 使用动态组件实现 Tab 在 Vue 中可以通过动态组件 <component :is="currentTab"> 结合 v-for 和 v-bind 实现 Ta…

vue实现曲线

vue实现曲线

Vue 实现曲线的方法 在 Vue 中实现曲线可以通过多种方式,包括使用 SVG、Canvas 或第三方库如 D3.js、ECharts 等。以下是几种常见的方法: 使用 SVG 绘制曲线 SVG…

vue实现视频

vue实现视频

Vue 实现视频播放的方法 在 Vue 项目中实现视频播放可以通过多种方式完成,以下是几种常见的方法: 使用 HTML5 的 <video> 标签 在 Vue 组件中直接使用 HTML…

vue登录实现

vue登录实现

Vue 登录实现 实现登录功能通常需要前端与后端配合,Vue 作为前端框架,主要负责表单处理、请求发送和状态管理。以下是常见的实现方式: 表单设计与数据绑定 使用 Vue 的 v-model 实现表…

vue系统实现

vue系统实现

Vue 系统实现的关键步骤 项目初始化与配置 使用 Vue CLI 或 Vite 创建项目,安装核心依赖(如 Vue 3、Vue Router、Pinia)。配置开发环境(如 ESLint、Prett…