当前位置:首页 > 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:

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

实现方法:

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实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式的…

vue实现granfana

vue实现granfana

Vue 实现类似 Grafana 的仪表盘 要在 Vue 中实现类似 Grafana 的仪表盘功能,需要结合数据可视化库、状态管理和 UI 组件。以下是关键步骤和推荐工具: 数据可视化库 使用 E…

vue实现前端

vue实现前端

Vue 实现前端的核心方法与步骤 安装 Vue 项目 使用 Vue CLI 或 Vite 创建项目。Vue CLI 是传统构建工具,Vite 是新一代轻量级工具。 npm init vue@la…

vue实现注销

vue实现注销

实现用户注销功能 在Vue应用中实现用户注销功能通常涉及清除用户会话、重置应用状态并跳转到登录页面。以下是具体实现方法: 清除用户认证信息 使用Vuex或Pinia管理用户状态时,需在注销时重置存储…

vue轮播实现

vue轮播实现

Vue 轮播实现方法 使用第三方库(推荐) 安装 swiper 或 vue-awesome-swiper 库,快速实现功能丰富的轮播效果。 以 vue-awesome-swiper 为例: npm…

vue实现menu

vue实现menu

Vue 实现 Menu 的方法 使用 Element UI 的 Menu 组件 Element UI 提供了现成的 Menu 组件,适合快速实现导航菜单。安装 Element UI 后,可以直接使用…