Vue3实现pdf在线预览的三种方式

 更新时间:2025年02月14日 10:20:33   作者:Li_Ning21  
这篇文章主要为大家详细介绍了使用Vue3实现pdf在线预览的三种常用方式,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

今天一天对当前可用的pdf预览插件做了测试,主要需求是只能预览不能下载,但对于前端来说,没有绝对的禁止,这里只罗列实现方式。

目前采用vue3版本为:3.2.37

  • iframe
  • vue-office
  • pdfjs-dist

iframe

先说最简单的,iframe可以直接展示pdf文件,所以如果不作禁止预览等操作,iframe是最合适的。

    <el-dialog
      v-model="previewOtherUpload"
      reset-drag-position
      draggable
      sticky
      :title="_options.imgName || '详情'"
      footer-hide
      class-name="vertical-center-modal"
    >
        <div
          @contextmenu.prevent
          style="user-select: none;"
        >
          <iframe
            ref="iframe"
            :src="`${modelValue}#toolbar=0`"
            width="100%"
            height="600px"
            @load="onIframeLoad"
          >
          </iframe>
        </div>
    </el-dialog>

<script setup>
const modelValue = ref('https://501351981.github.io/vue-office/examples/dist/static/test-files/test.pdf')
let previewOtherUpload = ref(false);
const iframe = ref(null)

const clickShow = () => {
	previewOtherUpload.value = true;
}

// 尝试在iframe加载完毕后,进行右键禁用,但实际需要通过postmessage来处理,所以这里无实际用处
const onIframeLoad = () => {
  try {
    console.log('iframe 已加载', iframe.value.contentWindow.window);
    if (iframe.value.contentWindow.document) {
      iframe.value.contentWindow.document.addEventListener('contextmenu', (e) => e.preventDefault());
    }
  } catch (error) {
    console.error('无法访问 iframe 内容:', error);
  }
}
</script>

vue-office

vue-office-gitcode地址

安装

#docx文档预览组件
npm install @vue-office/docx vue-demi@0.14.6

#excel文档预览组件
npm install @vue-office/excel vue-demi@0.14.6

#pdf文档预览组件
npm install @vue-office/pdf vue-demi@0.14.6

#pptx文档预览组件
npm install @vue-office/pptx vue-demi@0.14.6

如果是vue2.6版本或以下还需要额外安装 @vue/composition-api

npm install @vue/composition-api

我们如果只预览pdf,则安装 npm install @vue-office/pdf vue-demi@0.14.6

    <el-dialog
      v-model="previewOtherUpload"
      reset-drag-position
      draggable
      sticky
      :title="_options.imgName || '详情'"
      footer-hide
      class-name="vertical-center-modal"
    >
        <div
          @contextmenu.prevent
          style="user-select: none;"
        >
          <VueOfficePdf
            :src="modelValue"
          />
        </div>
    </el-dialog>

<script setup>
import VueOfficePdf from '@vue-office/pdf'
const modelValue = ref('https://501351981.github.io/vue-office/examples/dist/static/test-files/test.pdf')
let previewOtherUpload = ref(false);

const clickShow = () => {
	previewOtherUpload.value = true;
}
</script>

pdfjs-dist

这是目前最麻烦的一个插件,一定先确定下载的版本"pdfjs-dist": “2.16.105”,我用的是这个,否则下面的workerSrc设置会有问题。

  <el-dialog
      v-model="previewOtherUpload"
      reset-drag-position
      draggable
      sticky
      :title="_options.imgName || '详情'"
      footer-hide
      class-name="vertical-center-modal"
    >
		<div 
          id="pdf-view"
          @contextmenu.prevent
          style="user-select: none;"
        >
            <canvas v-for="page in state.pdfPages" :key="page" id="pdfCanvas" />
            <div id="text-view"></div>
        </div>
    </el-dialog>

<script setup>
import { computed, reactive, ref, watch, nextTick } from "vue";
import * as pdfjsViewer from 'pdfjs-dist/web/pdf_viewer.js'
import 'pdfjs-dist/web/pdf_viewer.css'
import * as PDF from 'pdfjs-dist'
// 设置 pdf.worker.js 路径
PDF.GlobalWorkerOptions.workerSrc = '../../../node_modules/pdfjs-dist/build/pdf.worker.js';
let pdfDoc = null;

const modelValue = ref('https://501351981.github.io/vue-office/examples/dist/static/test-files/test.pdf')
let previewOtherUpload = ref(false);

const clickShow = () => {
	loadFile(modelValue)
	previewOtherUpload.value = true;
}

const loadFile = (url) => {
    PDF.getDocument({
        url,
        cMapUrl: 'https://cdn.jsdelivr.net/npm/pdfjs-dist@2.16.105/cmaps/',
        cMapPacked: true,
    }).promise.then((pdf) => {
        pdfDoc = pdf
        // 获取pdf文件总页数
        state.pdfPages = pdf.numPages
        nextTick(() => {
            renderPage(1) // 从第一页开始渲染
        })
    })
}
const renderPage = (num) => {
    pdfDoc.getPage(num).then((page) => {
        const canvas = document.getElementById('pdfCanvas')
        const ctx = canvas.getContext('2d')
        const viewport = page.getViewport({ scale: state.pdfScale })
        canvas.width = viewport.width
        canvas.height = viewport.height
        const renderContext = {
            canvasContext: ctx,
            viewport
        }
        page.render(renderContext)
    })
}
</script>

插件样式也不好调整,不推荐。

总结

最后还是使用了第二种方式,作为禁止下载的展示。

以上就是Vue3实现pdf在线预览的三种方式的详细内容,更多关于Vue3在线预览pdf的资料请关注脚本之家其它相关文章!

相关文章

  • vue.js购物车添加商品组件的方法

    vue.js购物车添加商品组件的方法

    这篇文章主要介绍了vue.js购物车添加商品组件的方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下
    2019-09-09
  • 在Vue3中使用event bus(事件总线)的步骤详解

    在Vue3中使用event bus(事件总线)的步骤详解

    在Vue 3中,可以通过创建一个事件总线(event bus)来实现组件之间的通信,要在Vue 3中使用事件总线,主要步骤有:1、创建一个新的Vue实例作为事件总线,2、在组件中引入并使用该事件总线,以下是详细的步骤和示例,需要的朋友可以参考下
    2025-01-01
  • ElementUI修改实现更好用图片上传预览组件

    ElementUI修改实现更好用图片上传预览组件

    这篇文章主要为大家介绍了ElementUI修改实现更好用图片上传预览组件示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-09-09
  • Vue生命周期实例分析总结

    Vue生命周期实例分析总结

    Vue的生命周期就是vue实例从创建到销毁的全过程,也就是new Vue()开始就是vue生命周期的开始。Vue实例有⼀个完整的⽣命周期,也就是从开始创建、初始化数据、编译模版、挂载Dom->渲染、更新->渲染、卸载等⼀系列过程,称这是Vue的⽣命周期
    2022-10-10
  • vuex入门最详细整理

    vuex入门最详细整理

    在本篇文章里小编给大家分享的是关于vuex入门最详细整理的相关内容,需要的朋友们参考下。
    2020-03-03
  • 在Vue项目中集成和使用Lottie动画库的步骤详解

    在Vue项目中集成和使用Lottie动画库的步骤详解

    Lottie 是一个由 Airbnb 开源的动画库,它允许你在 Web、iOS、Android 等平台上使用体积小、高性能的体验丰富的矢量动画,本文将详细介绍在 Vue 项目中如何集成和使用 Lottie,文中有详细的代码讲解,需要的朋友可以参考下
    2023-11-11
  • Vue3系列之effect和ReactiveEffect track trigger源码解析

    Vue3系列之effect和ReactiveEffect track trigger源码解析

    这篇文章主要为大家介绍了Vue3系列之effect和ReactiveEffect track trigger源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-10-10
  • 详细聊聊Vue的混入和继承

    详细聊聊Vue的混入和继承

    混入(mixin)是一种非常灵活的方式,用来分发Vue组件中可复用的功能,一个混入对象可以包含任意组件选项,下面这篇文章主要给大家介绍了关于Vue混入和继承的相关资料,需要的朋友可以参考下
    2021-09-09
  • 使用 Vue cli 3.0 构建自定义组件库的方法

    使用 Vue cli 3.0 构建自定义组件库的方法

    本文旨在给大家提供一种构建一个完整 UI 库脚手架的思路。通过实例代码给大家讲解了使用 Vue cli 3.0 构建自定义组件库的方法,感兴趣的朋友跟随小编一起看看吧
    2019-04-04
  • 前端vue+elementUI如何实现记住密码功能

    前端vue+elementUI如何实现记住密码功能

    这篇文章主要给大家介绍了关于vue+elementUI如何实现记住密码功能的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09

最新评论