当前位置:首页 > VUE

VUE实现PDF打印页面

2026-01-23 09:48:38VUE

使用vue-pdf-embed组件实现PDF打印

安装vue-pdf-embed依赖包

npm install vue-pdf-embed

在Vue组件中引入并使用

<template>
  <div>
    <vue-pdf-embed :source="pdfUrl" ref="pdfRef" />
    <button @click="printPDF">打印PDF</button>
  </div>
</template>

<script>
import VuePdfEmbed from 'vue-pdf-embed'

export default {
  components: {
    VuePdfEmbed
  },
  data() {
    return {
      pdfUrl: '/path/to/your/file.pdf'
    }
  },
  methods: {
    printPDF() {
      window.print()
    }
  }
}
</script>

使用PDF.js库实现自定义打印

安装pdfjs-dist库

npm install pdfjs-dist

实现PDF渲染和打印功能

<template>
  <div>
    <div ref="pdfContainer"></div>
    <button @click="printPDF">打印</button>
  </div>
</template>

<script>
import * as pdfjsLib from 'pdfjs-dist'
import 'pdfjs-dist/web/pdf_viewer.css'

export default {
  data() {
    return {
      pdfUrl: '/path/to/your/file.pdf'
    }
  },
  methods: {
    async loadPDF() {
      const loadingTask = pdfjsLib.getDocument(this.pdfUrl)
      const pdf = await loadingTask.promise

      for (let i = 1; i <= pdf.numPages; i++) {
        const page = await pdf.getPage(i)
        const viewport = page.getViewport({ scale: 1.0 })

        const canvas = document.createElement('canvas')
        const context = canvas.getContext('2d')
        canvas.height = viewport.height
        canvas.width = viewport.width

        await page.render({
          canvasContext: context,
          viewport: viewport
        }).promise

        this.$refs.pdfContainer.appendChild(canvas)
      }
    },
    printPDF() {
      const printWindow = window.open('', '_blank')
      printWindow.document.write('<html><head><title>打印PDF</title></head><body>')
      printWindow.document.write(this.$refs.pdfContainer.innerHTML)
      printWindow.document.write('</body></html>')
      printWindow.document.close()
      printWindow.focus()
      printWindow.print()
    }
  },
  mounted() {
    this.loadPDF()
  }
}
</script>

使用iframe嵌入PDF实现打印

创建iframe组件打印PDF

<template>
  <div>
    <iframe 
      ref="pdfFrame" 
      :src="pdfUrl" 
      style="width: 100%; height: 800px; border: none;"
    ></iframe>
    <button @click="printPDF">打印</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      pdfUrl: '/path/to/your/file.pdf'
    }
  },
  methods: {
    printPDF() {
      this.$refs.pdfFrame.contentWindow.print()
    }
  }
}
</script>

使用CSS优化打印样式

添加打印样式表

<style scoped>
@media print {
  body * {
    visibility: hidden;
  }
  .pdf-container, .pdf-container * {
    visibility: visible;
  }
  .pdf-container {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
  }
  button {
    display: none;
  }
}
</style>

处理PDF文件下载与打印

从后端API获取PDF并打印

VUE实现PDF打印页面

<template>
  <div>
    <button @click="fetchAndPrintPDF">下载并打印PDF</button>
  </div>
</template>

<script>
export default {
  methods: {
    async fetchAndPrintPDF() {
      const response = await fetch('/api/get-pdf')
      const blob = await response.blob()
      const url = URL.createObjectURL(blob)

      const iframe = document.createElement('iframe')
      iframe.style.display = 'none'
      iframe.src = url
      document.body.appendChild(iframe)

      iframe.onload = () => {
        setTimeout(() => {
          iframe.contentWindow.print()
          URL.revokeObjectURL(url)
          document.body.removeChild(iframe)
        }, 500)
      }
    }
  }
}
</script>

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

相关文章

vue实现页面切换

vue实现页面切换

Vue 实现页面切换的方法 在 Vue 中实现页面切换通常可以通过以下几种方式完成,具体选择取决于项目需求和架构设计。 使用 Vue Router Vue Router 是 Vue.js 官方推荐的…

vue实现预约页面

vue实现预约页面

实现预约页面的基本结构 使用Vue CLI或Vite创建一个新项目,安装必要依赖如vue-router和axios。项目结构建议包含components文件夹存放可复用组件,views文件夹存放页面级…

vue文件实现页面跳转

vue文件实现页面跳转

使用 router-link 实现跳转 在 Vue 模板中直接使用 <router-link> 组件,通过 to 属性指定目标路径: <router-link to="/tar…

h5实现登录页面跳转页面跳转页面

h5实现登录页面跳转页面跳转页面

实现H5登录页面跳转 在H5中实现登录页面跳转可以通过多种方式完成,以下是几种常见方法: 使用window.location.href window.location.href = '目标页面UR…

如何react页面

如何react页面

创建 React 页面 使用 create-react-app 快速初始化项目: npx create-react-app my-app cd my-app npm start 基础页面结构 在 s…

vue页面实现pdf

vue页面实现pdf

在Vue中实现PDF功能 使用vue-pdf库 安装vue-pdf库: npm install vue-pdf 在Vue组件中使用: <template> <pdf :src…