当前位置:首页 > VUE

vue 实现pdf

2026-03-07 01:29:23VUE

Vue 中实现 PDF 预览与下载

使用 pdf.js 预览 PDF

安装 pdfjs-dist 库:

npm install pdfjs-dist

在 Vue 组件中引入并渲染 PDF:

<template>
  <div>
    <canvas ref="pdfCanvas"></canvas>
  </div>
</template>

<script>
import * as pdfjsLib from 'pdfjs-dist';

export default {
  props: ['pdfUrl'],
  mounted() {
    this.renderPdf();
  },
  methods: {
    async renderPdf() {
      const loadingTask = pdfjsLib.getDocument(this.pdfUrl);
      const pdf = await loadingTask.promise;
      const page = await pdf.getPage(1);
      const viewport = page.getViewport({ scale: 1.0 });
      const canvas = this.$refs.pdfCanvas;
      const context = canvas.getContext('2d');

      canvas.height = viewport.height;
      canvas.width = viewport.width;

      const renderContext = {
        canvasContext: context,
        viewport: viewport
      };
      page.render(renderContext);
    }
  }
};
</script>

使用 vue-pdf 插件

安装插件:

npm install vue-pdf

组件中使用:

<template>
  <pdf :src="pdfUrl"></pdf>
</template>

<script>
import pdf from 'vue-pdf';

export default {
  components: { pdf },
  data() {
    return {
      pdfUrl: '/sample.pdf'
    };
  }
};
</script>

实现 PDF 下载

通过 <a> 标签直接下载:

<template>
  <a :href="pdfUrl" download="document.pdf">Download PDF</a>
</template>

使用 Blob 对象动态下载:

<script>
export default {
  methods: {
    downloadPdf() {
      fetch('/sample.pdf')
        .then(response => response.blob())
        .then(blob => {
          const link = document.createElement('a');
          link.href = URL.createObjectURL(blob);
          link.download = 'document.pdf';
          link.click();
        });
    }
  }
};
</script>

服务端生成 PDF

使用 html2pdf.js 将 HTML 转为 PDF:

npm install html2pdf.js

组件示例:

vue 实现pdf

<template>
  <div ref="content">
    <!-- 需要转换为 PDF 的内容 -->
  </div>
  <button @click="generatePdf">Export PDF</button>
</template>

<script>
import html2pdf from 'html2pdf.js';

export default {
  methods: {
    generatePdf() {
      const element = this.$refs.content;
      const opt = {
        margin: 1,
        filename: 'document.pdf',
        image: { type: 'jpeg', quality: 0.98 },
        html2canvas: { scale: 2 },
        jsPDF: { unit: 'in', format: 'letter', orientation: 'portrait' }
      };
      html2pdf().from(element).set(opt).save();
    }
  }
};
</script>

注意事项

  • 跨域问题需确保服务器配置 CORS 头
  • 大文件 PDF 建议分页加载
  • 动态生成 PDF 时注意浏览器兼容性
  • 移动端需测试触控交互体验

标签: vuepdf
分享给朋友:

相关文章

vue实现选择本地文件

vue实现选择本地文件

实现文件选择功能 在Vue中实现本地文件选择可以通过HTML原生的<input type="file">元素实现。创建一个基本的文件选择组件: <template> &l…

vue 组件实现

vue 组件实现

Vue 组件实现方法 单文件组件 (SFC) 使用 .vue 文件格式,包含模板、脚本和样式三部分: <template> <div class="example">{{…

vue mvvm实现

vue mvvm实现

Vue MVVM 实现原理 Vue.js 是一个基于 MVVM(Model-View-ViewModel)模式的框架,其核心是通过数据绑定和响应式系统实现视图与数据的自动同步。以下是 Vue 实现 M…

vue实现路径

vue实现路径

Vue 实现路径的方法 在 Vue 中实现路径管理通常涉及 Vue Router 的使用,以下是一些常见的实现方法: 安装 Vue Router 通过 npm 或 yarn 安装 Vue Route…

vue 实现协同

vue 实现协同

Vue 实现协同编辑的方案 协同编辑指多个用户同时编辑同一文档并实时同步更改。Vue 结合相关库和技术可轻松实现该功能。 使用 WebSocket 实现实时通信 WebSocket 提供全双工通信,…

vue实现数据

vue实现数据

Vue 实现数据绑定的方法 Vue.js 提供了多种方式实现数据绑定,包括双向绑定、单向绑定以及动态数据绑定。以下是几种常见的实现方式: 双向数据绑定(v-model) 双向数据绑定通常用于表单元素…