当前位置:首页 > VUE

vue 实现pdf下载

2026-01-19 18:30:03VUE

Vue 实现 PDF 下载的方法

使用 jsPDF 库生成并下载 PDF

安装 jsPDF 库:

npm install jspdf

在 Vue 组件中使用 jsPDF

import { jsPDF } from "jspdf";

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

使用 pdf-lib 动态生成 PDF

安装 pdf-lib

npm install pdf-lib

示例代码:

import { PDFDocument, StandardFonts, rgb } from 'pdf-lib';

export default {
  methods: {
    async createPDF() {
      const pdfDoc = await PDFDocument.create();
      const page = pdfDoc.addPage();
      const { width, height } = page.getSize();

      const font = await pdfDoc.embedFont(StandardFonts.Helvetica);
      page.drawText('Vue PDF Download', {
        x: 50,
        y: height - 50,
        size: 30,
        font,
        color: rgb(0, 0, 0),
      });

      const pdfBytes = await pdfDoc.save();
      this.downloadPDF(pdfBytes, 'generated.pdf');
    },
    downloadPDF(pdfBytes, fileName) {
      const blob = new Blob([pdfBytes], { type: 'application/pdf' });
      const link = document.createElement('a');
      link.href = URL.createObjectURL(blob);
      link.download = fileName;
      link.click();
    }
  }
}

下载服务器上的 PDF 文件

如果 PDF 文件已存在于服务器:

export default {
  methods: {
    downloadServerPDF() {
      const link = document.createElement('a');
      link.href = '/path/to/file.pdf';
      link.download = 'downloaded.pdf';
      link.click();
    }
  }
}

使用 axios 下载二进制 PDF 数据

import axios from 'axios';

export default {
  methods: {
    async downloadPDF() {
      try {
        const response = await axios.get('/api/pdf', {
          responseType: 'blob'
        });
        const url = window.URL.createObjectURL(new Blob([response.data]));
        const link = document.createElement('a');
        link.href = url;
        link.setAttribute('download', 'file.pdf');
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      } catch (error) {
        console.error('Download failed:', error);
      }
    }
  }
}

使用 vue-pdf-embed 显示并下载 PDF

安装 vue-pdf-embed

npm install vue-pdf-embed

组件示例:

vue 实现pdf下载

import VuePdfEmbed from 'vue-pdf-embed';

export default {
  components: {
    VuePdfEmbed
  },
  methods: {
    downloadPDF() {
      const pdfElement = this.$refs.pdfEmbed;
      pdfElement.download();
    }
  }
}

注意事项

确保服务器正确设置 CORS 头部以允许文件下载 对于大文件下载,考虑显示进度提示 移动端设备可能需要特殊处理下载逻辑 某些浏览器可能会阻止自动下载,需要用户交互触发

标签: vuepdf
分享给朋友:

相关文章

vue实现oauth

vue实现oauth

Vue 实现 OAuth 的步骤 安装必要的依赖 确保项目中安装了 axios 和 vue-router。OAuth 通常需要发送 HTTP 请求和处理路由跳转。 npm install axios…

vue实现编辑

vue实现编辑

Vue 实现编辑功能 在 Vue 中实现编辑功能通常涉及表单绑定、状态管理和事件处理。以下是一个常见的实现方法: 双向数据绑定 使用 v-model 指令实现表单元素与数据的双向绑定: <i…

vue代理实现

vue代理实现

Vue 代理实现方法 在 Vue 项目中,配置代理可以解决开发环境下的跨域问题,通常通过修改 vue.config.js 文件实现。以下是几种常见的代理配置方式: 基础代理配置 在 vue.conf…

vue实现 单选

vue实现 单选

实现 Vue 单选功能 在 Vue 中实现单选功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-model 绑定单选按钮 通过 v-model 可以轻松实现单选功能,将单选按钮的值绑定到同…

vue实现爬虫

vue实现爬虫

Vue 实现爬虫的基本思路 Vue.js 本身是一个前端框架,主要用于构建用户界面。要实现爬虫功能,通常需要结合后端技术或浏览器自动化工具。以下是几种常见的方法: 方法一:Vue + Node.js…

vue实现页面

vue实现页面

Vue 实现页面的基本方法 创建 Vue 项目 使用 Vue CLI 快速初始化项目,运行以下命令安装并创建项目: npm install -g @vue/cli vue create my-pro…