当前位置:首页 > VUE

vue实现预览pdf

2026-02-20 10:47:04VUE

使用 vue-pdf 库实现 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: '/path/to/document.pdf'
    }
  }
}
</script>

使用 PDF.js 实现自定义 PDF 预览

安装 pdfjs-dist:

npm install pdfjs-dist

实现基础预览功能:

<template>
  <div>
    <canvas ref="pdfCanvas"></canvas>
    <button @click="prevPage">上一页</button>
    <button @click="nextPage">下一页</button>
  </div>
</template>

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

export default {
  data() {
    return {
      pdfDoc: null,
      pageNum: 1,
      pageRendering: false,
      pageNumPending: null,
      scale: 1.5
    }
  },
  mounted() {
    this.loadPDF('/path/to/document.pdf')
  },
  methods: {
    async loadPDF(url) {
      const loadingTask = pdfjsLib.getDocument(url)
      this.pdfDoc = await loadingTask.promise
      this.renderPage(this.pageNum)
    },
    async renderPage(num) {
      this.pageRendering = true
      const page = await this.pdfDoc.getPage(num)
      const viewport = page.getViewport({ scale: this.scale })
      const canvas = this.$refs.pdfCanvas
      const context = canvas.getContext('2d')

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

      const renderContext = {
        canvasContext: context,
        viewport: viewport
      }

      await page.render(renderContext).promise
      this.pageRendering = false
    },
    prevPage() {
      if (this.pageNum <= 1) return
      this.pageNum--
      this.renderPage(this.pageNum)
    },
    nextPage() {
      if (this.pageNum >= this.pdfDoc.numPages) return
      this.pageNum++
      this.renderPage(this.pageNum)
    }
  }
}
</script>

使用 iframe 嵌入 PDF 预览

简单实现方案:

<template>
  <iframe 
    src="/path/to/document.pdf" 
    width="100%" 
    height="600px"
    style="border: none;">
  </iframe>
</template>

使用 Mozilla 的 PDF Viewer

通过 object 标签实现:

<template>
  <object 
    data="/path/to/document.pdf" 
    type="application/pdf"
    width="100%" 
    height="600px">
    <p>您的浏览器不支持PDF预览,请<a :href="pdfUrl">下载PDF</a></p>
  </object>
</template>

<script>
export default {
  data() {
    return {
      pdfUrl: '/path/to/document.pdf'
    }
  }
}
</script>

实现 PDF 缩略图导航

扩展 PDF.js 实现缩略图:

<template>
  <div class="pdf-container">
    <div class="thumbnail-container">
      <div 
        v-for="page in totalPages" 
        :key="page"
        @click="goToPage(page)"
        class="thumbnail">
        <canvas :ref="`thumb${page}`"></canvas>
      </div>
    </div>
    <div class="main-view">
      <canvas ref="pdfCanvas"></canvas>
    </div>
  </div>
</template>

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

export default {
  data() {
    return {
      pdfDoc: null,
      totalPages: 0,
      currentPage: 1
    }
  },
  async mounted() {
    await this.loadPDF('/path/to/document.pdf')
    this.renderThumbnails()
  },
  methods: {
    async loadPDF(url) {
      const loadingTask = pdfjsLib.getDocument(url)
      this.pdfDoc = await loadingTask.promise
      this.totalPages = this.pdfDoc.numPages
      this.renderPage(this.currentPage)
    },
    async renderPage(num) {
      const page = await this.pdfDoc.getPage(num)
      // ...渲染主视图逻辑
    },
    async renderThumbnails() {
      for (let i = 1; i <= this.totalPages; i++) {
        const page = await this.pdfDoc.getPage(i)
        const viewport = page.getViewport({ scale: 0.5 })
        const canvas = this.$refs[`thumb${i}`][0]
        const context = canvas.getContext('2d')

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

        await page.render({
          canvasContext: context,
          viewport: viewport
        }).promise
      }
    },
    goToPage(pageNum) {
      this.currentPage = pageNum
      this.renderPage(pageNum)
    }
  }
}
</script>

<style>
.pdf-container {
  display: flex;
}
.thumbnail-container {
  width: 20%;
}
.thumbnail {
  margin-bottom: 10px;
  cursor: pointer;
}
.main-view {
  width: 80%;
}
</style>

vue实现预览pdf

标签: vuepdf
分享给朋友:

相关文章

vue实现导航切换内容

vue实现导航切换内容

Vue实现导航切换内容的方法 在Vue中实现导航切换内容,可以通过以下方式实现: 使用动态组件 动态组件通过<component :is="currentComponent">实现内容…

vue实现div

vue实现div

Vue 实现 DIV 元素的方法 在 Vue 中实现 DIV 元素可以通过多种方式,以下是几种常见的实现方法: 使用模板语法 在 Vue 的模板中直接使用 HTML 的 <div> 标签…

vue 实现权限

vue 实现权限

Vue 实现权限控制的方法 在 Vue 项目中实现权限控制通常涉及前端路由、组件和按钮级别的权限管理。以下是几种常见的实现方式: 路由权限控制 通过路由守卫实现权限验证,过滤用户无权访问的路由:…

vue实现节流

vue实现节流

vue实现节流的方法 在Vue中实现节流(throttle)功能,通常用于限制高频事件的触发频率,例如滚动、输入或按钮点击。以下是几种常见的实现方式: 使用Lodash的throttle函数 安装L…

vue 实现脚本

vue 实现脚本

Vue 实现脚本的方法 Vue.js 提供了多种方式来实现脚本功能,包括组件内脚本、混入(Mixins)、插件(Plugins)以及自定义指令等。以下是常见的实现方式: 组件内脚本 在 Vue 单文…

vue实现录像

vue实现录像

Vue 实现录像功能 在 Vue 中实现录像功能通常需要借助浏览器的 MediaDevices API 和 MediaRecorder API。以下是实现步骤: 获取用户摄像头和麦克风权限 使用…