【vue3】实现pdf在线预览的几种方式

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

  1. iframe
  2. vue-office
  3. 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 {
   
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值