当前位置:首页 > VUE

vue页面实现pdf

2026-01-15 07:50:31VUE

在Vue中实现PDF功能

使用vue-pdf库

安装vue-pdf库:

npm install vue-pdf

在Vue组件中使用:

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

<script>
import pdf from 'vue-pdf'

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

使用PDF.js直接集成

安装pdfjs-dist:

vue页面实现pdf

npm install pdfjs-dist

实现代码:

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

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

export default {
  mounted() {
    this.renderPDF()
  },
  methods: {
    async renderPDF() {
      const loadingTask = pdfjsLib.getDocument('/path/to/document.pdf')
      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
      }
      await page.render(renderContext).promise
    }
  }
}
</script>

生成PDF文件

使用jsPDF库生成PDF:

vue页面实现pdf

npm install jspdf

生成PDF示例:

<script>
import jsPDF from 'jspdf'

export default {
  methods: {
    generatePDF() {
      const doc = new jsPDF()
      doc.text('Hello world!', 10, 10)
      doc.save('document.pdf')
    }
  }
}
</script>

显示PDF下载链接

简单实现方式:

<template>
  <a :href="pdfUrl" download>下载PDF</a>
</template>

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

高级功能实现

添加分页控制:

<template>
  <div>
    <pdf :src="pdfSrc" :page="currentPage"></pdf>
    <button @click="prevPage">上一页</button>
    <button @click="nextPage">下一页</button>
  </div>
</template>

<script>
import pdf from 'vue-pdf'

export default {
  components: { pdf },
  data() {
    return {
      pdfSrc: '/path/to/document.pdf',
      currentPage: 1,
      pageCount: 0
    }
  },
  mounted() {
    this.getPageCount()
  },
  methods: {
    async getPageCount() {
      const loadingTask = pdf.createLoadingTask(this.pdfSrc)
      const pdf = await loadingTask.promise
      this.pageCount = pdf.numPages
    },
    prevPage() {
      if (this.currentPage > 1) this.currentPage--
    },
    nextPage() {
      if (this.currentPage < this.pageCount) this.currentPage++
    }
  }
}
</script>

以上方法涵盖了从简单显示到高级控制的多种PDF实现方案,可以根据具体需求选择合适的实现方式。

标签: 页面vue
分享给朋友:

相关文章

vue实现fadein

vue实现fadein

Vue 实现 FadeIn 效果 在 Vue 中实现淡入(FadeIn)效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法: 使用 CSS 过渡 通过 Vue 的过渡系统结合 CSS…

vue  实现tab

vue 实现tab

实现 Tab 切换功能 在 Vue 中实现 Tab 切换功能可以通过动态组件或条件渲染完成。以下是两种常见方法: 使用 v-if 或 v-show 实现条件渲染 通过绑定 currentTab 变量…

vue 实现后退

vue 实现后退

实现后退功能的方法 在Vue中实现后退功能通常需要结合浏览器历史记录API或Vue Router的导航方法。以下是几种常见的实现方式: 使用Vue Router的go方法 this.$router…

vue实现气泡

vue实现气泡

Vue 实现气泡效果的方法 在 Vue 中实现气泡效果可以通过 CSS 动画、第三方库或自定义组件完成。以下是几种常见实现方式: 使用纯 CSS 和 Vue 过渡 通过 Vue 的过渡系统结合 CS…

vue实现slidedown

vue实现slidedown

Vue 实现 SlideDown 动画效果 在 Vue 中实现 SlideDown 效果可以通过 CSS 过渡或动画结合 Vue 的过渡系统完成。以下是几种常见方法: 使用 Vue Transiti…

vue实现选择置顶

vue实现选择置顶

Vue 实现选择置顶功能 在 Vue 中实现选择置顶功能,可以通过操作数据数组或结合 CSS 样式来实现。以下是几种常见的实现方法: 数组排序法 通过操作数据数组,将被选中的项移动到数组首位: /…