当前位置:首页 > VUE

vue实现标签打印

2026-03-09 20:08:13VUE

Vue 实现标签打印的方法

使用浏览器原生打印功能

通过调用浏览器的 window.print() 方法实现打印功能。这种方法简单快捷,适合打印静态内容。

<template>
  <div>
    <div ref="printContent" class="print-area">
      <!-- 需要打印的内容 -->
      <h3>标签内容</h3>
      <p>示例标签文本</p>
    </div>
    <button @click="handlePrint">打印标签</button>
  </div>
</template>

<script>
export default {
  methods: {
    handlePrint() {
      window.print();
    }
  }
};
</script>

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

使用第三方库 vue-print-nb

vue-print-nb 是一个专门为 Vue 设计的打印插件,支持自定义打印内容和样式。

安装依赖:

npm install vue-print-nb --save

在 Vue 项目中使用:

<template>
  <div>
    <div id="printContent" class="print-area">
      <!-- 需要打印的内容 -->
      <h3>标签内容</h3>
      <p>示例标签文本</p>
    </div>
    <button v-print="printConfig">打印标签</button>
  </div>
</template>

<script>
import print from 'vue-print-nb';

export default {
  directives: {
    print
  },
  data() {
    return {
      printConfig: {
        id: 'printContent',
        popTitle: '标签打印', // 打印标题
        extraCss: 'https://example.com/print.css', // 额外样式
        extraHead: '<meta http-equiv="Content-Language" content="zh-cn">' // 额外头部信息
      }
    };
  }
};
</script>

使用 html2canvas 和 jsPDF 生成 PDF 打印

如果需要将标签内容转换为 PDF 再打印,可以使用 html2canvasjsPDF 组合。

安装依赖:

npm install html2canvas jspdf --save

实现代码:

vue实现标签打印

<template>
  <div>
    <div ref="printContent" class="print-area">
      <!-- 需要打印的内容 -->
      <h3>标签内容</h3>
      <p>示例标签文本</p>
    </div>
    <button @click="generatePDF">生成PDF并打印</button>
  </div>
</template>

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

export default {
  methods: {
    async generatePDF() {
      const element = this.$refs.printContent;
      const canvas = await html2canvas(element);
      const imgData = canvas.toDataURL('image/png');
      const pdf = new jsPDF('p', 'mm', 'a4');
      const imgWidth = 190; // PDF 页面宽度
      const imgHeight = (canvas.height * imgWidth) / canvas.width;
      pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight);
      pdf.save('label.pdf');
      // 直接打印
      pdf.autoPrint();
      window.open(pdf.output('bloburl'), '_blank');
    }
  }
};
</script>

注意事项

  • 打印样式需要使用 @media print 来定义,确保打印效果符合预期。
  • 打印内容需要精确控制尺寸时,建议使用固定单位(如 mmpt)。
  • 某些浏览器可能会阻止自动弹出打印窗口,需要用户手动触发。
  • 测试不同浏览器的兼容性,尤其是 Safari 和 Firefox 可能存在差异。

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

相关文章

vue实现anchor

vue实现anchor

Vue 实现 Anchor(锚点)功能 在 Vue 中实现锚点功能可以通过多种方式完成,以下是几种常见的方法: 使用 HTML 原生锚点 HTML 原生锚点是最简单的方式,通过 <a>…

课程表vue实现

课程表vue实现

实现课程表的Vue组件 创建课程表需要设计数据结构、布局和交互逻辑。以下是一个基于Vue 3的实现方案: 数据结构设计 const timetableData = ref([ { ti…

vue实现滑块

vue实现滑块

Vue 实现滑块组件的方法 使用原生 HTML 和 Vue 指令 通过 Vue 的 v-model 和事件监听实现基础滑块功能。创建一个包含 input 元素的组件,类型设置为 range,并绑定到…

vue翻页实现

vue翻页实现

Vue翻页实现方法 基础分页组件实现 使用v-for和计算属性实现基础分页逻辑: <template> <div> <ul> <li…

vue实现swiper

vue实现swiper

Vue 中实现 Swiper 的方法 安装 Swiper 依赖 在 Vue 项目中安装 Swiper 和相关依赖: npm install swiper vue-awesome-swiper 全局引…

vue 实现$(id)

vue 实现$(id)

在 Vue 中实现类似 jQuery 的 $(id) 功能,可以通过以下几种方式操作 DOM 元素: 使用 ref 属性 Vue 推荐使用 ref 属性来直接访问 DOM 元素或组件实例。在模板中为…