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

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:

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页面实现pdf

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

相关文章

vue如何实现到期提醒

vue如何实现到期提醒

实现 Vue 到期提醒功能 使用计算属性计算剩余时间 在 Vue 组件中定义一个计算属性,计算目标日期与当前日期的差值。通过 Date 对象获取时间戳并计算剩余天数或小时数。 computed:…

vue实现a

vue实现a

Vue 实现 A 功能的方法 在 Vue 中实现 A 功能可以通过多种方式,具体取决于 A 功能的具体需求。以下是几种常见的实现方法: 使用 Vue 组件 通过创建自定义组件来实现 A 功能。组件…

vue实现slider

vue实现slider

Vue 实现 Slider 组件 使用 Vue 实现 Slider 组件可以通过原生开发或借助第三方库。以下是两种常见方法: 原生实现 Slider 创建一个基础的 Slider 组件,通过 v-m…

vue实现画图

vue实现画图

Vue 实现画图功能 在 Vue 中实现画图功能通常需要结合 HTML5 的 <canvas> 元素或第三方库。以下是几种常见的方法: 使用原生 Canvas API 通过 Vue 直接…

vue实现pie

vue实现pie

Vue 实现饼图 在 Vue 中实现饼图通常可以通过第三方图表库(如 ECharts、Chart.js 或 Highcharts)来完成。以下是使用 ECharts 和 Chart.js 的两种常见方…

vue alert实现

vue alert实现

使用 Vue 实现 Alert 组件 在 Vue 中实现 Alert 组件可以通过自定义组件或结合第三方库完成。以下是几种常见方法: 自定义 Alert 组件 创建一个可复用的 Alert 组件,…