当前位置:首页 > VUE

vue实现发票打印

2026-03-09 18:36:50VUE

Vue 实现发票打印方案

在 Vue 中实现发票打印功能通常需要结合浏览器打印 API 和样式控制。以下是几种常见实现方式:

方案一:使用 window.print() 原生打印

创建独立的发票打印组件,通过 CSS 控制打印样式:

<template>
  <div class="invoice-print">
    <button @click="printInvoice">打印发票</button>
    <div class="invoice-content" ref="invoiceContent">
      <!-- 发票内容结构 -->
      <h3>发票抬头</h3>
      <table>
        <!-- 表格内容 -->
      </table>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    printInvoice() {
      const printContent = this.$refs.invoiceContent.innerHTML;
      const originalContent = document.body.innerHTML;

      document.body.innerHTML = printContent;
      window.print();
      document.body.innerHTML = originalContent;
    }
  }
}
</script>

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

方案二:使用 html2canvas + jsPDF 生成 PDF

适用于需要精确控制打印格式或生成 PDF 文件的场景:

npm install html2canvas jspdf

组件实现:

vue实现发票打印

<template>
  <div>
    <button @click="exportToPDF">导出PDF</button>
    <div id="invoiceTemplate">
      <!-- 发票模板 -->
    </div>
  </div>
</template>

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

export default {
  methods: {
    async exportToPDF() {
      const element = document.getElementById('invoiceTemplate');
      const canvas = await html2canvas(element, {
        scale: 2,
        logging: false,
        useCORS: true
      });

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

      pdf.addImage(imgData, 'PNG', 0, 0, imgWidth, imgHeight);
      pdf.save('invoice.pdf');
    }
  }
}
</script>

方案三:使用专用打印插件

对于复杂打印需求,可以考虑专用打印库:

npm install vue-print-nb

使用示例:

vue实现发票打印

import Print from 'vue-print-nb';

Vue.use(Print);

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

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

关键注意事项

打印样式必须通过 @media print 专门定义,普通样式在打印时可能无效。建议设置:

@media print {
  @page {
    size: A4 portrait;
    margin: 0;
  }
  body {
    -webkit-print-color-adjust: exact;
  }
  .no-print {
    display: none !important;
  }
}

对于中国发票特有的需求,如发票号码、二维码、印章等,需要特别注意:

  • 使用固定定位确保元素位置精确
  • 二维码建议使用 canvas 绘制
  • 印章可以使用透明 PNG 叠加

响应式打印处理

针对不同尺寸纸张的适配方案:

function getPrintStyle(pageSize = 'A4') {
  const sizes = {
    'A4': { width: '210mm', height: '297mm' },
    'A5': { width: '148mm', height: '210mm' }
  };

  return `
    @page {
      size: ${sizes[pageSize].width} ${sizes[pageSize].height};
      margin: 10mm;
    }
  `;
}

以上方案可根据具体业务需求组合使用,建议先进行打印预览测试,确保各浏览器兼容性。

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

相关文章

vue实现flbook

vue实现flbook

Vue 实现类似 Flbook 的翻页效果 要实现类似 Flbook 的翻页效果,可以使用 Vue 结合 CSS 动画和 JavaScript 事件处理。以下是实现方法: 安装依赖 需要安装 vue…

vue mvvm 实现

vue mvvm 实现

Vue MVVM 实现原理 Vue.js 的 MVVM(Model-View-ViewModel)架构通过数据绑定和响应式系统实现视图与数据的自动同步。以下是其核心实现机制: 数据劫持与响应式系统…

vue实现拖拉

vue实现拖拉

实现拖拽功能的基本步骤 在Vue中实现拖拽功能可以通过原生HTML5的拖放API或第三方库如vuedraggable完成。以下是两种方法的详细说明。 使用HTML5原生拖放API HTML5提供了原…

vue实现 页面

vue实现 页面

Vue 实现页面的核心方法 创建 Vue 项目 使用 Vue CLI 或 Vite 快速初始化项目。Vue CLI 是官方脚手架工具,适合传统项目;Vite 提供更快的现代构建体验。 npm cre…

vue实现绘图

vue实现绘图

Vue 实现绘图的方法 在 Vue 中实现绘图功能,可以通过多种方式实现,包括使用原生 HTML5 Canvas、第三方库(如 Fabric.js、Konva.js)或 SVG。以下是几种常见的实现方…

vue实现全屏滚动

vue实现全屏滚动

实现全屏滚动的 Vue 方法 使用第三方库 vue-fullpage.js 安装 vue-fullpage.js: npm install vue-fullpage.js 在 Vue 项目中引入并注…