当前位置:首页 > 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 插件

安装插件:

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> 标签直接下载:

vue 实现pdf

<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

组件示例:

<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 中实现画圆弧并着色 使用 Canvas API 绘制圆弧 在 Vue 组件的 mounted 钩子中,通过 Canvas API 绘制圆弧。创建一个 canvas 元素并获取其上下文:…

vue实现picker

vue实现picker

Vue 实现 Picker 组件的方法 在 Vue 中实现 Picker 组件可以通过多种方式完成,以下是几种常见的实现方法: 使用原生 HTML 和 CSS 实现 通过 Vue 的模板和样式绑定,…

vue实现频道

vue实现频道

Vue实现频道功能的方法 使用Vue Router动态路由 通过Vue Router的动态路由功能可以轻松实现频道切换。在路由配置中定义动态参数,组件内通过this.$route.params获取当前…

vue如何实现

vue如何实现

Vue 实现方法 在 Vue 中实现功能通常涉及组件化开发、状态管理和响应式数据绑定。以下是几种常见的实现方式: 组件化开发 使用 Vue 的单文件组件(.vue 文件)结构,将 UI 拆分为可复用…

vue mvvm实现

vue mvvm实现

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

vue实现伸缩

vue实现伸缩

Vue实现伸缩功能的方法 使用CSS过渡和Vue的v-if或v-show 通过CSS的transition属性结合Vue的条件渲染指令(v-if或v-show)可以实现元素的伸缩效果。定义一个CSS类…