当前位置:首页 > VUE

vue实现报表打印

2026-02-17 03:02:27VUE

Vue 实现报表打印的方法

使用 window.print() 实现基础打印

在 Vue 中可以直接调用浏览器的打印功能。创建一个隐藏的打印区域,通过 CSS 控制打印样式。

<template>
  <div>
    <button @click="printReport">打印报表</button>
    <div id="printArea" class="print-only">
      <!-- 报表内容 -->
      <h1>销售报表</h1>
      <table>
        <!-- 表格数据 -->
      </table>
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    printReport() {
      const printContents = document.getElementById('printArea').innerHTML;
      const originalContents = document.body.innerHTML;
      document.body.innerHTML = printContents;
      window.print();
      document.body.innerHTML = originalContents;
    }
  }
}
</script>

<style>
@media print {
  .no-print {
    display: none;
  }
  .print-only {
    display: block;
  }
}
</style>

使用 vue-print-nb 插件

vue-print-nb 是一个专门为 Vue 设计的打印插件,使用更简单。

安装插件:

npm install vue-print-nb --save

使用示例:

vue实现报表打印

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

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

export default {
  directives: {
    print
  },
  data() {
    return {
      printObj: {
        id: 'printMe',
        popTitle: '报表打印',
        extraCss: 'https://example.com/print.css'
      }
    }
  }
}
</script>

使用 PDF 生成后打印

对于复杂报表,可先生成 PDF 再打印。使用 jsPDF 或 html2pdf 等库。

安装 html2pdf:

npm install html2pdf.js

使用示例:

vue实现报表打印

<template>
  <div>
    <button @click="generatePDF">生成PDF并打印</button>
    <div id="pdfContent">
      <!-- 报表内容 -->
    </div>
  </div>
</template>

<script>
import html2pdf from 'html2pdf.js'

export default {
  methods: {
    generatePDF() {
      const element = document.getElementById('pdfContent');
      const opt = {
        margin: 10,
        filename: 'report.pdf',
        image: { type: 'jpeg', quality: 0.98 },
        html2canvas: { scale: 2 },
        jsPDF: { unit: 'mm', format: 'a4', orientation: 'portrait' }
      };

      html2pdf().from(element).set(opt).save().then(() => {
        // 打印PDF
        window.print();
      });
    }
  }
}
</script>

使用打印样式优化

通过 CSS 媒体查询专门为打印设计样式,确保打印效果符合要求。

@media print {
  body {
    font-size: 12pt;
    color: #000;
  }
  table {
    width: 100%;
    border-collapse: collapse;
  }
  th, td {
    border: 1px solid #ddd;
    padding: 8px;
  }
  .no-print {
    display: none !important;
  }
  @page {
    size: A4;
    margin: 10mm;
  }
}

分页控制

对于长报表,使用 CSS 控制分页:

.page-break {
  page-break-after: always;
}

在需要分页的位置添加:

<div class="page-break"></div>

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

相关文章

vue实现密码框的包

vue实现密码框的包

Vue 密码框实现方案 在 Vue 中实现密码框功能,可以通过原生 HTML 结合 Vue 指令或使用第三方组件库完成。以下是几种常见方案: 原生 HTML + Vue 双向绑定 <templ…

vue实现矩阵

vue实现矩阵

Vue 实现矩阵的方法 在 Vue 中实现矩阵操作可以通过多种方式完成,以下是一些常见的方法: 使用二维数组表示矩阵 矩阵可以用二维数组来表示,每个元素对应矩阵中的一个值。在 Vue 的 data…

vue实现 toast

vue实现 toast

Vue 实现 Toast 组件 在 Vue 中实现 Toast 组件可以通过多种方式完成,以下是几种常见的方法: 使用全局组件和事件总线 创建一个全局的 Toast 组件,并通过事件总线或 Vue…

vue实现gps

vue实现gps

Vue 中实现 GPS 定位功能 在 Vue 中实现 GPS 定位功能通常依赖于浏览器的 Geolocation API 或第三方地图服务(如高德、百度地图等)。以下是两种常见的实现方式: 使用浏览…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选择…

vue 实现ztree

vue 实现ztree

在Vue中实现类似ZTree的功能,可以通过集成第三方库或自定义组件完成。以下是两种常见方法: 使用vue-tree组件 vue-tree是一个轻量级的Vue树形组件,支持复选框、懒加载等功能,适合…