当前位置:首页 > VUE

vue 实现导出pdf

2026-01-08 08:40:03VUE

使用 html2canvas 和 jsPDF 实现导出 PDF

在 Vue 项目中安装 html2canvas 和 jsPDF 依赖:

npm install html2canvas jspdf

创建一个方法将指定 DOM 元素转换为 PDF:

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

export default {
  methods: {
    exportToPDF() {
      const element = document.getElementById('pdf-content')
      html2canvas(element).then(canvas => {
        const imgData = canvas.toDataURL('image/png')
        const pdf = new jsPDF('p', 'mm', 'a4')
        const imgWidth = 210
        const pageHeight = 295
        const imgHeight = canvas.height * imgWidth / canvas.width
        let heightLeft = imgHeight
        let position = 0

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

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

        pdf.save('export.pdf')
      })
    }
  }
}

使用 vue-html2pdf 插件

安装 vue-html2pdf 插件:

npm install vue-html2pdf

在组件中使用:

import VueHtml2pdf from 'vue-html2pdf'

export default {
  components: {
    VueHtml2pdf
  },
  methods: {
    generateReport() {
      this.$refs.html2Pdf.generatePdf()
    }
  }
}

模板部分:

<template>
  <div>
    <button @click="generateReport">导出PDF</button>
    <vue-html2pdf
      ref="html2Pdf"
      :filename="'report.pdf'"
      :paginate-elements-by-height="1400"
      :pdf-quality="2"
      :manual-pagination="false"
      pdf-format="a4"
      pdf-orientation="portrait"
    >
      <section slot="pdf-content">
        <!-- 这里放要导出的内容 -->
        <div id="pdf-content">...</div>
      </section>
    </vue-html2pdf>
  </div>
</template>

使用 PDFMake 库实现

安装 pdfmake 依赖:

npm install pdfmake

创建 PDF 内容:

import pdfMake from 'pdfmake/build/pdfmake'
import pdfFonts from 'pdfmake/build/vfs_fonts'
pdfMake.vfs = pdfFonts.pdfMake.vfs

export default {
  methods: {
    exportPDF() {
      const docDefinition = {
        content: [
          { text: 'PDF标题', style: 'header' },
          '这里是PDF内容...',
          { text: '表格示例', style: 'subheader' },
          {
            table: {
              body: [
                ['列1', '列2', '列3'],
                ['值1', '值2', '值3']
              ]
            }
          }
        ],
        styles: {
          header: { fontSize: 18, bold: true },
          subheader: { fontSize: 15, bold: true }
        }
      }
      pdfMake.createPdf(docDefinition).download('document.pdf')
    }
  }
}

使用浏览器打印功能

通过 CSS 媒体查询优化打印样式:

@media print {
  body * {
    visibility: hidden;
  }
  #print-area, #print-area * {
    visibility: visible;
  }
  #print-area {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
  }
}

调用浏览器打印 API:

export default {
  methods: {
    printPDF() {
      window.print()
    }
  }
}

注意事项

确保导出的内容在 DOM 中完全渲染后再执行导出操作,可以在 nextTick 中调用导出方法:

this.$nextTick(() => {
  this.exportToPDF()
})

对于复杂布局,可能需要调整 html2canvas 的配置参数:

html2canvas(element, {
  scale: 2,  // 提高分辨率
  logging: false,
  useCORS: true,
  allowTaint: true
})

中文显示问题可以通过引入中文字体解决,在 jsPDF 中:

vue 实现导出pdf

const pdf = new jsPDF({
  orientation: 'p',
  unit: 'mm',
  format: 'a4',
  hotfixes: ['px_scaling']
})
pdf.addFont('simhei.ttf', 'simhei', 'normal')
pdf.setFont('simhei')

标签: vuepdf
分享给朋友:

相关文章

vue 实现级联

vue 实现级联

Vue 实现级联选择器 级联选择器(Cascader)常用于省市区选择、分类选择等场景。Vue 中可以通过 Element UI、Ant Design Vue 等 UI 库实现,也可以手动封装。 使…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue多选实现

vue多选实现

Vue 多选实现方法 基础实现(v-model + multiple) 在 Vue 中实现多选可以通过 <select> 元素的 multiple 属性结合 v-model 完成。v-mo…

vue cli 实现

vue cli 实现

Vue CLI 实现步骤 安装 Vue CLI 确保 Node.js 版本在 8.9 或以上。通过 npm 全局安装 Vue CLI: npm install -g @vue/cli 创建新项目 使…

vue nexttrick实现

vue nexttrick实现

Vue 中 nextTick 的实现原理 nextTick 是 Vue 提供的一个异步方法,用于在下次 DOM 更新循环结束之后执行延迟回调。它利用了 JavaScript 的事件循环机制,确保回调在…

vue实现导航

vue实现导航

使用 Vue Router 实现导航 Vue Router 是 Vue.js 官方提供的路由管理器,用于构建单页应用(SPA)的导航系统。以下是实现导航的基本步骤。 安装 Vue Router n…