vue3使用pdfh5(安卓端)

29 篇文章 1 订阅
文章介绍了在Vue3项目中使用pdfh5库来展示PDF文件的步骤,包括安装pdfh5、引入库、设置页面元素、初始化插件以及处理iOS设备的PDF预览。在iOS上,利用a标签的点击事件直接预览,而安卓则通过pdfh5打开新页面进行预览。
摘要由CSDN通过智能技术生成

1.安装pdfh5

npm install pdfh5

2.引入pdfh5

import Pdfh5 from 'pdfh5';
import 'pdfh5/css/pdfh5.css';

3.页面

<div ref="refPdf" class="refPdf"></div>

4. js

import { defineComponent, onMounted, ref } from 'vue';
setup() {
    const route = useRoute();
    const refPdf = ref(null);
    //页面加载
    onMounted(() => {
      let href = route.query.url;
      const pdfh5 = new Pdfh5(refPdf.value, {
        pdfurl: href
      });
      pdfh5.on('complete', (status, msg, time) => {
        console.log(
          '状态:' + status + ',信息:' + msg + ',耗时:' + time + '毫秒'
        );
      });
    });
    return {
      refPdf
    };
  }

5.ios

//pdf预览
const previewPdf = (val) =>{
	let u = navigator.userAgent;
      let isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
      if (isiOS) {
        //a标签打开
         var a = document.createElement("a");
    let href = val;
    a.setAttribute("href", href);
    a.setAttribute("target", "_blank");
    a.setAttribute("id", "startTelMedicine");
    a.onclick = function () {
        //关闭窗口的方法
        window.opener = null;
        window.open("", "_self", "");
        window.close();
    };
    // 防止反复添加
    if (document.getElementById("startTelMedicine")) {
        document.body.removeChild(document.getElementById("startTelMedicine"));
    }
    document.body.appendChild(a);
    a.click();
      } else {
        router.push({
          path: '/pdf',
          query: {
            url: store.IMAGEURL + href
          }
        });
      }
}

总结:ios手机自带pdf预览功能,a标签打开即可预览,安卓手机需要本文使用pdfh5进行预览(打开了新页面)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值