当前位置:首页 > VUE

vue实现标签打印

2026-03-30 07:51:52VUE

使用vue-print-nb插件实现标签打印

安装vue-print-nb插件

npm install vue-print-nb --save

在main.js中引入并注册插件

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

在组件中使用v-print指令

vue实现标签打印

<template>
  <div>
    <button v-print="printObj">打印标签</button>
    <div id="printContent">
      <!-- 标签内容 -->
      <div class="label">标签内容示例</div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      printObj: {
        id: 'printContent',
        popTitle: '标签打印',
        extraCss: 'https://example.com/print.css'
      }
    }
  }
}
</script>

<style>
@media print {
  .label {
    width: 100mm;
    height: 50mm;
    border: 1px solid #000;
    page-break-after: always;
  }
}
</style>

使用window.print()原生方法实现

创建打印模板组件

<template>
  <div>
    <button @click="printLabel">打印标签</button>
    <div ref="printContent" class="print-area">
      <div v-for="(item, index) in labels" :key="index" class="label">
        {{ item.content }}
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      labels: [
        { content: '标签1' },
        { content: '标签2' }
      ]
    }
  },
  methods: {
    printLabel() {
      const printWindow = window.open('', '_blank')
      printWindow.document.write(`
        <html>
          <head>
            <title>标签打印</title>
            <style>
              .label {
                width: 100mm;
                height: 50mm;
                border: 1px solid #000;
                margin: 5mm;
                page-break-after: always;
              }
            </style>
          </head>
          <body>
            ${this.$refs.printContent.innerHTML}
          </body>
        </html>
      `)
      printWindow.document.close()
      printWindow.focus()
      printWindow.print()
      printWindow.close()
    }
  }
}
</script>

使用CSS控制打印样式

设置打印专用样式

vue实现标签打印

@media print {
  body * {
    visibility: hidden;
  }
  .print-area, .print-area * {
    visibility: visible;
  }
  .print-area {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
  }
  .label {
    width: 100mm;
    height: 50mm;
    border: 1px solid #000;
    margin: 5mm;
    page-break-after: always;
  }
}

处理多页标签打印

配置分页和边距

@page {
  size: A4;
  margin: 0;
}
@media print {
  .label {
    width: 90mm;
    height: 50mm;
    margin: 5mm;
    float: left;
    page-break-inside: avoid;
  }
}

使用PDF生成后打印

安装html2pdf.js

npm install html2pdf.js

在组件中使用

import html2pdf from 'html2pdf.js'

export default {
  methods: {
    printToPDF() {
      const element = document.getElementById('printContent')
      const opt = {
        margin: 10,
        filename: 'labels.pdf',
        image: { type: 'jpeg', quality: 0.98 },
        html2canvas: { scale: 2 },
        jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' }
      }
      html2pdf().from(element).set(opt).save()
    }
  }
}

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

相关文章

vue里实现增删改查

vue里实现增删改查

在Vue中实现增删改查 数据初始化 在Vue组件的data中定义初始数据和表单绑定的变量: data() { return { items: [ { id: 1, name:…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue实现toggle

vue实现toggle

Vue 实现 Toggle 功能 在 Vue 中实现 toggle(切换)功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 和 v-on 通过 v-model 绑定数据,结合…

vue实现github

vue实现github

Vue 实现 GitHub 相关功能 创建 GitHub API 请求 在 Vue 项目中,可以使用 axios 或 fetch 与 GitHub API 进行交互。需要在 GitHub 上创建个人访…

vue实现listview

vue实现listview

Vue 实现 ListView 的方法 基础列表渲染 使用 v-for 指令可以轻松实现基础列表渲染。在 Vue 中,v-for 用于遍历数组或对象,生成动态列表。 <template>…