当前位置:首页 > VUE

vue实现PDF导出

2026-02-19 13:58:50VUE

使用 vue-pdf 库导出 PDF

安装 vue-pdf 库

npm install @tboerc/vue-pdf --save

在组件中引入并使用

import { VuePdf } from '@tboerc/vue-pdf';

export default {
  components: {
    VuePdf
  },
  methods: {
    exportPDF() {
      this.$refs.pdfComponent.download('filename.pdf');
    }
  }
}

模板中使用

<vue-pdf ref="pdfComponent" :src="pdfSource"></vue-pdf>
<button @click="exportPDF">导出PDF</button>

使用 jsPDF 库生成 PDF

安装 jsPDF

npm install jspdf --save

基本使用方法

vue实现PDF导出

import jsPDF from 'jspdf';

export default {
  methods: {
    generatePDF() {
      const doc = new jsPDF();
      doc.text('Hello world!', 10, 10);
      doc.save('document.pdf');
    }
  }
}

添加图片和样式

const doc = new jsPDF();
doc.setFontSize(16);
doc.text('标题', 105, 15, { align: 'center' });
doc.addImage(imageData, 'JPEG', 15, 40, 180, 160);

使用 html2canvas 和 jsPDF 结合导出 HTML

安装依赖

npm install html2canvas jspdf --save

实现方法

vue实现PDF导出

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

export default {
  methods: {
    exportHTMLToPDF() {
      const element = document.getElementById('content-to-export');

      html2canvas(element).then(canvas => {
        const imgData = canvas.toDataURL('image/png');
        const pdf = new jsPDF('p', 'mm', 'a4');
        const imgProps = pdf.getImageProperties(imgData);
        const pdfWidth = pdf.internal.pageSize.getWidth();
        const pdfHeight = (imgProps.height * pdfWidth) / imgProps.width;

        pdf.addImage(imgData, 'PNG', 0, 0, pdfWidth, pdfHeight);
        pdf.save('export.pdf');
      });
    }
  }
}

使用 pdfmake 客户端生成 PDF

安装 pdfmake

npm install pdfmake --save

基本使用示例

import pdfMake from 'pdfmake/build/pdfmake';
import pdfFonts from 'pdfmake/build/vfs_fonts';

pdfMake.vfs = pdfFonts.pdfMake.vfs;

export default {
  methods: {
    createPDF() {
      const docDefinition = {
        content: [
          { text: 'PDF标题', style: 'header' },
          '这是正文内容...'
        ],
        styles: {
          header: {
            fontSize: 18,
            bold: true
          }
        }
      };

      pdfMake.createPdf(docDefinition).download('document.pdf');
    }
  }
}

服务器端生成 PDF

使用 axios 请求服务器

import axios from 'axios';

export default {
  methods: {
    requestServerPDF() {
      axios({
        url: '/api/generate-pdf',
        method: 'POST',
        responseType: 'blob',
        data: { content: 'PDF内容' }
      }).then(response => {
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'server.pdf');
        document.body.appendChild(link);
        link.click();
      });
    }
  }
}

每种方法适用于不同场景,vue-pdf适合直接显示和导出现有PDF,jsPDF适合动态生成简单PDF,html2canvas+jsPDF组合适合将HTML转为PDF,pdfmake提供更丰富的样式控制,而服务器端方案适合复杂PDF生成需求。

标签: vuePDF
分享给朋友:

相关文章

vue实现单点

vue实现单点

Vue 实现单点登录(SSO)方案 单点登录(SSO)允许用户在多个相关但独立的系统中使用同一组凭证登录。以下是基于 Vue 的实现方案: 基于 OAuth2/OpenID Connect 的实现…

eventbus vue实现

eventbus vue实现

EventBus 实现原理 EventBus 是一种发布/订阅模式,用于 Vue 组件间的通信。核心是通过一个中央事件总线管理事件监听和触发。 创建 EventBus 在 Vue 项目中创建一个独立…

vue实现方法

vue实现方法

Vue 实现方法 Vue 是一种流行的前端框架,用于构建用户界面和单页应用。以下是几种常见的 Vue 实现方法: 创建 Vue 实例 通过 new Vue() 创建一个 Vue 实例,传入配置对象,…

vue实现muli

vue实现muli

Vue 实现多语言(i18n)支持 在 Vue 项目中实现多语言(i18n)功能,通常使用 vue-i18n 插件。以下是具体实现步骤: 安装 vue-i18n npm install vue-i…

vue实现同步

vue实现同步

Vue 实现同步的方法 在 Vue 中实现同步操作通常涉及数据绑定、状态管理和异步操作的处理。以下是几种常见的方法: 使用计算属性(Computed Properties) 计算属性基于响应式依赖…

vue 实现全屏

vue 实现全屏

Vue 实现全屏功能 在 Vue 中实现全屏功能可以通过浏览器提供的 Fullscreen API 完成。以下是一个完整的实现方案: 安装依赖(可选) 如果需要跨浏览器兼容性更好,可以安装 scr…