当前位置:首页 > VUE

vue实现发票预览

2026-03-09 16:22:25VUE

Vue 实现发票预览功能

发票预览功能通常需要结合前端展示和数据处理,以下是实现的具体方法:

使用动态模板渲染

在Vue组件中设计发票的HTML/CSS模板,通过v-for、v-if等指令动态填充数据。例如发票项目列表:

vue实现发票预览

<div class="invoice-preview">
  <div class="header">
    <h2>{{ invoice.title }}</h2>
    <p>编号:{{ invoice.number }}</p>
  </div>

  <table class="items">
    <tr v-for="(item, index) in invoice.items" :key="index">
      <td>{{ item.name }}</td>
      <td>{{ item.quantity }}</td>
      <td>{{ item.price }}</td>
      <td>{{ item.quantity * item.price }}</td>
    </tr>
  </table>

  <div class="total">
    总计:{{ calculateTotal() }}
  </div>
</div>

数据绑定与计算属性

通过Vue的响应式系统管理发票数据,使用计算属性处理合计金额等逻辑:

export default {
  data() {
    return {
      invoice: {
        title: '增值税发票',
        number: 'INV20230001',
        items: [
          { name: '服务费', quantity: 2, price: 500 },
          { name: '产品A', quantity: 1, price: 1200 }
        ]
      }
    }
  },
  methods: {
    calculateTotal() {
      return this.invoice.items.reduce((sum, item) => {
        return sum + (item.quantity * item.price)
      }, 0)
    }
  }
}

PDF导出功能

如需生成PDF,可使用html2pdf.js等库:

vue实现发票预览

import html2pdf from 'html2pdf.js'

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

样式设计

通过CSS确保打印和屏幕显示效果一致:

.invoice-preview {
  width: 210mm;
  min-height: 297mm;
  padding: 20mm;
  margin: 0 auto;
  box-shadow: 0 0 10px rgba(0,0,0,0.1);
  background: white;
}

@media print {
  body * {
    visibility: hidden;
  }
  .invoice-preview, .invoice-preview * {
    visibility: visible;
  }
  .invoice-preview {
    position: absolute;
    left: 0;
    top: 0;
  }
}

第三方组件集成

对于复杂需求,可考虑专用发票组件库如vue-invoice-preview,或使用PDF.js直接渲染已有PDF发票:

import { PDFDocumentProxy } from 'pdfjs-dist'

export default {
  methods: {
    async renderPDF(url) {
      const loadingTask = pdfjsLib.getDocument(url)
      const pdf = await loadingTask.promise
      const page = await pdf.getPage(1)

      const viewport = page.getViewport({ scale: 1.0 })
      const canvas = this.$refs.canvas
      const context = canvas.getContext('2d')

      canvas.height = viewport.height
      canvas.width = viewport.width

      const renderContext = {
        canvasContext: context,
        viewport: viewport
      }
      page.render(renderContext)
    }
  }
}

以上方案可根据实际需求组合使用,动态模板适合定制化发票,PDF方案适合已有模板的渲染。

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

相关文章

vue实现tree

vue实现tree

Vue 实现 Tree 组件 使用 Vue 实现 Tree 组件可以通过递归组件的方式来实现层级结构展示。以下是一个完整的实现方法: 基础递归组件实现 创建 Tree 组件文件 Tree.vue,使…

vue核心实现

vue核心实现

Vue 核心实现原理 Vue.js 的核心实现主要包括响应式系统、虚拟 DOM、模板编译和组件化机制。以下是关键实现原理的详细说明: 响应式系统 Vue 使用 Object.defineProper…

vue实现注册

vue实现注册

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

vue filter 实现

vue filter 实现

在 Vue.js 中,过滤器(Filter)是一种用于文本格式化的功能,可以在模板中直接使用。以下是实现过滤器的详细方法: 全局过滤器注册 通过 Vue.filter 方法注册全局过滤器,可在所有组…

vue实现翻页

vue实现翻页

Vue实现翻页功能 在Vue中实现翻页功能通常需要结合分页组件和数据请求逻辑。以下是常见的实现方法: 使用Element UI分页组件 安装Element UI后,可以直接使用其分页组件:…

vue watch实现

vue watch实现

在 Vue 中实现 watch 监听数据变化 Vue 的 watch 选项用于监听数据变化并执行相应的回调函数。以下是几种常见的实现方式: 基础用法 export default { data(…