当前位置:首页 > 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实现引导

vue实现引导

Vue 实现引导功能的方法 使用第三方库 推荐使用 driver.js 或 intro.js 这类专门为 Vue 设计的引导库。这些库提供了完整的 API 和样式,能够快速实现引导功能。 安装 dr…

vue实现类别

vue实现类别

Vue 实现类别的几种方法 在 Vue 中实现类别功能可以通过多种方式完成,以下是一些常见的方法: 使用 v-bind:class 动态绑定类名 通过对象语法或数组语法动态绑定类名,根据条件添加或移…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类 通…

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,通…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue实现toast

vue实现toast

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