当前位置:首页 > VUE

vue实现报表打印

2026-03-29 02:01:11VUE

使用Vue实现报表打印功能

在Vue项目中实现报表打印功能,可以通过以下几种方法实现:

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

创建一个隐藏的打印区域,通过CSS控制打印样式,调用浏览器原生打印功能。

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

<script>
export default {
  methods: {
    printReport() {
      const printContent = this.$refs.printContent.innerHTML
      const originalContent = document.body.innerHTML
      document.body.innerHTML = printContent
      window.print()
      document.body.innerHTML = originalContent
    }
  }
}
</script>

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

方法二:使用vue-print-nb插件

vue实现报表打印

安装vue-print-nb插件可以更简单地实现打印功能。

npm install vue-print-nb --save

在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: '销售报表'
      }
    }
  }
}
</script>

方法三:使用html2canvas和jsPDF生成PDF

vue实现报表打印

对于需要生成PDF格式的报表,可以使用html2canvas和jsPDF库。

npm install html2canvas jspdf --save

实现代码:

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

<template>
  <div>
    <button @click="exportToPDF">导出PDF</button>
    <div ref="pdfContent">
      <!-- 报表内容 -->
    </div>
  </div>
</template>

<script>
export default {
  methods: {
    exportToPDF() {
      html2canvas(this.$refs.pdfContent).then(canvas => {
        const imgData = canvas.toDataURL('image/png')
        const pdf = new jsPDF('p', 'mm', 'a4')
        const imgWidth = 190
        const pageHeight = 277
        const imgHeight = canvas.height * imgWidth / canvas.width
        let heightLeft = imgHeight
        let position = 10

        pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight)
        heightLeft -= pageHeight

        while (heightLeft >= 0) {
          position = heightLeft - imgHeight
          pdf.addPage()
          pdf.addImage(imgData, 'PNG', 10, position, imgWidth, imgHeight)
          heightLeft -= pageHeight
        }

        pdf.save('report.pdf')
      })
    }
  }
}
</script>

优化打印样式的CSS技巧

@media print {
  @page {
    size: A4;
    margin: 0;
  }
  body {
    margin: 1.6cm;
  }
  .no-print {
    display: none !important;
  }
  table {
    width: 100%;
    border-collapse: collapse;
  }
  th, td {
    border: 1px solid #000;
    padding: 8px;
  }
}

以上方法可以根据具体需求选择使用,原生打印适合简单报表,插件方式更便捷,而PDF导出适合需要保存电子文档的场景。

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

相关文章

vue实现图集

vue实现图集

Vue 实现图集的方法 在 Vue 中实现图集功能可以通过多种方式完成,以下是一些常见的方法和实现步骤。 使用第三方库(如 vue-image-lightbox) 安装 vue-image-ligh…

vue实现sso

vue实现sso

Vue 实现 SSO(单点登录)方案 单点登录(SSO)允许用户通过一次登录访问多个相关系统。以下是基于 Vue 的实现方案: 使用 OAuth2/OpenID Connect 协议 OAuth2…

vue实现报表

vue实现报表

vue实现报表的方法 使用Vue实现报表通常需要结合图表库或表格组件。以下是几种常见方法: 使用ECharts 安装ECharts库: npm install echarts vue-echart…

vue实现xss

vue实现xss

XSS 攻击简介 XSS(跨站脚本攻击)是一种常见的安全漏洞,攻击者通过注入恶意脚本到网页中,当其他用户访问该页面时,脚本会在其浏览器中执行,可能导致数据泄露或会话劫持。 Vue 中的 XSS…

vue实现图册

vue实现图册

Vue实现图册功能 在Vue中实现图册功能通常涉及图片展示、切换、缩放等交互效果。以下是常见的实现方法和关键代码示例: 基础图册组件结构 使用Vue单文件组件构建基础结构: <templat…

vue jwt实现

vue jwt实现

Vue JWT 实现方法 安装依赖 确保项目中安装了 jsonwebtoken(后端)和 axios(前端)。若使用 Vue 3,可搭配 vue-router 和 pinia(或 vuex)管理状态。…