当前位置:首页 > VUE

vue实现发票打印

2026-02-18 05:42:08VUE

vue实现发票打印

在Vue中实现发票打印功能,通常需要结合前端打印库和样式设计。以下是实现步骤:

使用vue-print-nb插件

安装vue-print-nb插件:

npm install vue-print-nb --save

在main.js中引入并注册:

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

在模板中使用:

<button v-print="printObj">打印</button>
<div id="printContent">
  <!-- 发票内容 -->
</div>

在data中定义printObj:

vue实现发票打印

data() {
  return {
    printObj: {
      id: 'printContent',
      popTitle: '发票打印'
    }
  }
}

自定义打印样式

在CSS中定义打印专用样式:

@media print {
  @page {
    size: A4;
    margin: 0;
  }
  body * {
    visibility: hidden;
  }
  #printContent, #printContent * {
    visibility: visible;
  }
  #printContent {
    position: absolute;
    left: 0;
    top: 0;
    width: 210mm;
    padding: 10mm;
    box-sizing: border-box;
  }
}

使用html2canvas和jsPDF实现PDF打印

安装依赖:

npm install html2canvas jspdf --save

实现方法:

vue实现发票打印

import html2canvas from 'html2canvas'
import jsPDF from 'jspdf'

methods: {
  async printPDF() {
    const element = document.getElementById('printContent')
    const canvas = await html2canvas(element, {
      scale: 2,
      logging: false,
      useCORS: true
    })

    const imgData = canvas.toDataURL('image/png')
    const pdf = new jsPDF('p', 'mm', 'a4')
    const imgWidth = 190
    const imgHeight = canvas.height * imgWidth / canvas.width

    pdf.addImage(imgData, 'PNG', 10, 10, imgWidth, imgHeight)
    pdf.save('invoice.pdf')
  }
}

发票模板设计

发票HTML结构示例:

<div id="printContent" class="invoice-template">
  <div class="invoice-header">
    <h2>增值税普通发票</h2>
    <div class="invoice-number">发票号码:{{ invoiceNo }}</div>
  </div>

  <div class="invoice-body">
    <table>
      <tr>
        <th>商品名称</th>
        <th>规格</th>
        <th>数量</th>
        <th>单价</th>
        <th>金额</th>
      </tr>
      <tr v-for="item in items" :key="item.id">
        <td>{{ item.name }}</td>
        <td>{{ item.spec }}</td>
        <td>{{ item.quantity }}</td>
        <td>{{ item.price }}</td>
        <td>{{ item.amount }}</td>
      </tr>
    </table>
  </div>

  <div class="invoice-footer">
    <div>合计金额:{{ totalAmount }}</div>
    <div>开票日期:{{ invoiceDate }}</div>
  </div>
</div>

注意事项

打印前确保所有动态数据已加载完成,可以在打印方法中添加数据检查逻辑

对于复杂发票样式,建议使用固定尺寸布局(如A4纸尺寸210mm×297mm)

测试不同浏览器的打印兼容性,特别是IE浏览器可能需要特殊处理

考虑添加打印预览功能,提升用户体验

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

相关文章

用vue实现搜索查询

用vue实现搜索查询

实现搜索查询的基本思路 在Vue中实现搜索查询功能通常涉及以下几个核心部分:数据绑定、事件监听、过滤逻辑和UI反馈。以下是一个完整的实现方案。 数据绑定与输入框 创建一个Vue组件,包含一个输入框…

vue实现标题

vue实现标题

Vue 实现标题的方法 在Vue中实现标题可以通过多种方式,以下是几种常见的方法: 方法一:使用模板语法 在Vue组件的模板中直接使用HTML的<h1>到<h6>标签来…

vue 实现登录

vue 实现登录

Vue 实现登录功能 创建登录表单组件 在 Vue 项目中创建一个登录表单组件(如 Login.vue),包含用户名和密码输入框以及提交按钮。使用 v-model 实现双向数据绑定。 <te…

vue实现路径

vue实现路径

Vue 实现路径的方法 在 Vue 中实现路径管理通常涉及 Vue Router 的使用,以下是一些常见的实现方法: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Rout…

vue实现注册

vue实现注册

Vue 实现注册功能 注册功能通常涉及前端表单、数据验证、与后端 API 的交互等步骤。以下是一个完整的 Vue 实现注册功能的方案。 创建注册表单 使用 Vue 的模板语法创建注册表单,包含用户…

vue实现增加

vue实现增加

实现 Vue 中的新增功能 在 Vue 中实现新增功能通常涉及数据绑定、表单处理和状态管理。以下是几种常见的实现方式: 数据绑定与列表渲染 通过 v-model 绑定表单输入,使用数组的 pu…