当前位置:首页 > VUE

vue 实现pdf

2026-01-12 18:26:09VUE

在Vue中实现PDF功能

使用vue-pdf库

安装vue-pdf库:

npm install vue-pdf

在Vue组件中使用:

vue 实现pdf

<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

实现PDF渲染:

vue 实现pdf

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

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

export default {
  data() {
    return {
      pdfDoc: null,
      pageNum: 1,
      pageRendering: false
    }
  },
  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: 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
      this.pageRendering = false
    }
  }
}
</script>

实现PDF下载功能

<template>
  <button @click="downloadPDF">下载PDF</button>
</template>

<script>
export default {
  methods: {
    downloadPDF() {
      const link = document.createElement('a')
      link.href = '/path/to/document.pdf'
      link.download = 'document.pdf'
      document.body.appendChild(link)
      link.click()
      document.body.removeChild(link)
    }
  }
}
</script>

使用第三方服务生成PDF

安装html2pdf.js:

npm install html2pdf.js

将HTML内容转换为PDF:

<template>
  <div ref="content">
    <!-- 要转换为PDF的内容 -->
  </div>
  <button @click="generatePDF">生成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>

实现PDF预览与分页控制

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

<script>
import pdf from 'vue-pdf'

export default {
  components: { pdf },
  data() {
    return {
      pdfSrc: '/path/to/document.pdf',
      currentPage: 1,
      pageCount: 0
    }
  },
  created() {
    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>

标签: vuepdf
分享给朋友:

相关文章

vue实现菜单

vue实现菜单

Vue 实现菜单的方法 在 Vue 中实现菜单功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-for 动态生成菜单项 通过数据驱动的方式,利用 v-for 指令动态渲染菜单项。定义一个…

vue实现选区创建

vue实现选区创建

Vue 实现选区创建的方法 在 Vue 中实现选区创建通常涉及 DOM 操作和事件处理。以下是几种常见的方法: 使用原生 DOM API 通过 window.getSelection() 和 Ra…

vue实现路由导航

vue实现路由导航

路由导航的实现方式 在Vue中,路由导航主要通过Vue Router实现。Vue Router是Vue.js官方的路由管理器,用于构建单页面应用(SPA)。 基本路由配置 安装Vue Router后…

vue实现条件判断

vue实现条件判断

Vue 条件判断实现方法 Vue 提供了多种方式实现条件判断,可以根据不同场景选择合适的方式。 v-if / v-else-if / v-else 这是 Vue 中最常用的条件渲染指令,根据表达式的…

vue实现好评弹框

vue实现好评弹框

Vue 实现好评弹框的方法 使用 Vue 实现好评弹框可以通过组件化方式完成,结合动态数据绑定和事件处理。以下是具体实现步骤: 1. 创建弹框组件 新建一个 RatingDialog.vue 组件…

vue怎么实现选中删除

vue怎么实现选中删除

实现选中删除功能的方法 在Vue中实现选中删除功能通常涉及以下几个关键步骤: 数据绑定与选中状态管理 使用v-model或v-for指令绑定列表数据,结合复选框或单选按钮实现选中状态管理。例如:…