当前位置:首页 > VUE

vue实现pdf浏览

2026-02-18 05:34:31VUE

使用 vue-pdf 库实现 PDF 预览

vue-pdf 是一个轻量级的 Vue 组件,专门用于在 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: 'https://example.com/sample.pdf'
    }
  }
}
</script>

使用 PDF.js 实现高级功能

Mozilla 的 PDF.js 提供更完整的 PDF 渲染功能,支持分页、缩放等特性。安装核心库:

vue实现pdf浏览

npm install pdfjs-dist

完整实现示例:

<template>
  <div>
    <canvas v-for="page in numPages" :key="page" :ref="'canvas'+page"></canvas>
  </div>
</template>

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

export default {
  data() {
    return {
      pdfDoc: null,
      numPages: 0
    }
  },
  mounted() {
    this.loadPDF('https://example.com/sample.pdf')
  },
  methods: {
    async loadPDF(url) {
      const loadingTask = pdfjsLib.getDocument(url)
      this.pdfDoc = await loadingTask.promise
      this.numPages = this.pdfDoc.numPages

      for(let i = 1; i <= this.numPages; i++) {
        const page = await this.pdfDoc.getPage(i)
        const viewport = page.getViewport({ scale: 1.0 })
        const canvas = this.$refs['canvas'+i][0]
        const context = canvas.getContext('2d')

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

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

添加 PDF 控制功能

为 PDF 查看器添加导航控制:

vue实现pdf浏览

<template>
  <div>
    <button @click="prevPage" :disabled="currentPage <= 1">上一页</button>
    <span>第 {{ currentPage }} 页 / 共 {{ numPages }} 页</span>
    <button @click="nextPage" :disabled="currentPage >= numPages">下一页</button>
    <pdf :src="pdfUrl" :page="currentPage"></pdf>
  </div>
</template>

<script>
import pdf from 'vue-pdf'

export default {
  components: { pdf },
  data() {
    return {
      pdfUrl: 'https://example.com/sample.pdf',
      currentPage: 1,
      numPages: 0
    }
  },
  mounted() {
    const loadingTask = pdf.createLoadingTask(this.pdfUrl)
    loadingTask.then(pdf => {
      this.numPages = pdf.numPages
    })
  },
  methods: {
    prevPage() {
      if(this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if(this.currentPage < this.numPages) this.currentPage++
    }
  }
}
</script>

处理本地 PDF 文件

实现本地文件上传预览:

<template>
  <div>
    <input type="file" @change="onFileChange" accept=".pdf">
    <pdf :src="pdfFile" v-if="pdfFile"></pdf>
  </div>
</template>

<script>
import pdf from 'vue-pdf'

export default {
  components: { pdf },
  data() {
    return {
      pdfFile: null
    }
  },
  methods: {
    onFileChange(e) {
      const file = e.target.files[0]
      if(file.type !== 'application/pdf') {
        alert('请选择PDF文件')
        return
      }
      this.pdfFile = URL.createObjectURL(file)
    }
  }
}
</script>

优化性能的注意事项

对于大型PDF文件,建议实现分页加载而不是一次性渲染所有页面。可以使用懒加载技术,只在用户滚动到可视区域时才渲染对应页面。

添加错误处理逻辑,处理可能出现的PDF加载失败情况。PDF.js支持设置密码保护文档的解密功能,需要处理密码输入场景。

标签: vuepdf
分享给朋友:

相关文章

vue实现高亮

vue实现高亮

Vue 实现文本高亮的方法 在 Vue 中实现文本高亮可以通过多种方式完成,以下是几种常见的方法: 方法一:使用 v-html 指令 通过 v-html 指令可以动态插入 HTML 内容,将需要高…

vue实现hover

vue实现hover

Vue 实现 hover 效果的方法 在 Vue 中实现 hover 效果可以通过多种方式实现,包括使用 CSS、Vue 指令或事件监听。以下是几种常见的方法: 使用 CSS :hover 伪类 通…

vue实现frame

vue实现frame

Vue 中实现 iframe 的方法 在 Vue 中可以通过直接使用 <iframe> 标签或动态绑定 src 属性来实现 iframe 功能。 基本用法 <template&g…

vue实现粘贴

vue实现粘贴

Vue 实现粘贴功能的方法 在 Vue 中实现粘贴功能通常涉及监听粘贴事件并处理剪贴板数据。以下是几种常见的实现方式: 监听原生粘贴事件 通过 @paste 指令或原生 addEventListen…

vue 实现打印

vue 实现打印

Vue 实现打印功能的方法 在Vue项目中实现打印功能,可以通过以下几种方式实现: 使用window.print()方法 通过调用浏览器的原生打印API实现基础打印功能,适用于简单内容打印。 //…

vue实现treeview

vue实现treeview

Vue 实现 TreeView 的方法 使用递归组件实现 TreeView 递归组件是 Vue 中实现 TreeView 的常见方式。通过组件调用自身,可以轻松处理嵌套的树形结构数据。 <te…