Uniapp 实现预览pdf 文件

采用 pdfh5 组件完成隐私协议、合同文档的 PDF 在线预览功能,提供页面滚动到底部、计时读秒双重校验规则,以此确认用户已完整查阅文件内容。

具体实现:

导入pdfh5库

复制代码
npm install pdfh5

修改pdf 多页阅读时,当前页码错误问题:

pdfh5.js 文件修改如下:

javascript 复制代码
   //FIXME svg scroll 无效, uniapp使用 onPageScroll onReachBottom 
     viewerContainer.addEventListener('scroll', function () {
     	var scrollTop = viewerContainer.scrollTop;
       console.log(`scroll event ${scrollTop}`);
     	if (scrollTop >= 150) {
     		if (self.options.backTop) {
     			self.backTop.show();
     		}
     	} else {
     		if (self.options.backTop) {
     			self.backTop.fadeOut(200);
     		}
     	}
     	if (self.viewerContainer) {
     		self.pages = self.viewerContainer.find('.pageContainer');
     	}
     	clearTimeout(self.timer);
     	if (self.options.pageNum && self.pageNum) {
     		self.pageNum.show();
     	}
     	var h = containerH;
     	if (self.pages) {
     		self.pages.each(function (index, obj) {
     			var top = obj.getBoundingClientRect().top;
     			var bottom = obj.getBoundingClientRect().bottom;
     			if (top <= height && bottom > height) {
     				if (self.options.pageNum) {
     					self.pageNow.text(index + 1);
     				}
     				self.currentNum = index + 1;
     			}
     			if (top <= h && bottom > h) {
     				self.cacheNum = index + 1;
     			}
     		});
     	}
     	if (scrollTop + self.container.height() >= self.viewer[0].offsetHeight) {
     		self.pageNow.text(self.totalNum);
//阅读到最后一页 把当前页进行赋值
        self.currentNum =self.totalNum
     	}
     	if (scrollTop === 0) {
     		self.pageNow.text(1);
     	}
     	self.timer = setTimeout(function () {
     		if (self.options.pageNum && self.pageNum) {
     			self.pageNum.fadeOut(200);
     		}
     	}, 1500);
      
      // console.log(`pdfh5:1309 pageNow=${self.pageNow.text()} pageNum=${self.pageNum.text()} currentNum = ${self.currentNum}`);
     	var arr1 = self.eventType["scroll"];
     	if (arr1 && arr1 instanceof Array) {
     		for (var i = 0; i < arr1.length; i++) {
     			arr1[i] && arr1[i].call(self, scrollTop, self.currentNum);
     		}
     	}
     });
  

具体组件使用:

javascript 复制代码
<template>
  <view class="pdf-page">
    <view class="pdf-wrap" :style="{'height':viewHeight}">
      <!-- :style="{'height': pdfviewHeight}" -->
      <view ref="pdfRef" id="pdfView" :style="{'height':viewHeight}"></view>
    </view>
    <view v-if="showBottom" class="pdf-btn-group">
      <slot>
        <button class="btn-readed" size="mini" @click="handleComplete" :style="btnStyle"
            :disabled="btnDisabled">{{showTimer ? countTimer<=1 ? '':'('+countTimer+'s)':''}}我已阅读</button>
      </slot>
    </view>
  </view>
</template>

<script>
  import Pdfh5 from "pdfh5";
  import "pdfh5/css/pdfh5.css";
  export default {
    name: "u-pdf",
    props: {
      url: {
        type: String,
        require: true
      },
      viewHeight: { // 高度设置后可能会影响到滚动条的监听,需要使用pdf总页数和当前页码对比
        type: String,
        default: "100%"
      },
      showBottom: {
        type: Boolean,
        default: true
      },
      options: {
        type: Object,
        default: () => ({
          cMapUrl: "https://unpkg.com/pdfjs-dist@3.8.162/cmaps/",
          lazy: false,
          withCredentials: true,
          // pdfLoaded:false,
          renderType: "svg",
          maxZoom: 3, //手势缩放最大倍数 默认3
          scrollEnable: true, //是否允许pdf滚动
          zoomEnable: true //是否允许pdf手势缩放
        })
      },
      disabled:{
        type:Boolean,
        default:true
      },
      showTimer:Boolean,
      readTimer:{
        type:Number,
        default:20
      },
      btnStyle:{
        type:Object,
        default:()=>{}
      }
    },
    data() {
      return {
        totalPages: 0,
        pdfh5: undefined,
        countTimer:20,
        timer:undefined,
        btnDisabled:true,
        
      }
    },
    
    computed:{
    },
    
    watch:{
      disabled(n,o){
        this.btnDisabled = this.disabled
      },
      showTimer(n,o){
        console.log("showTimer ",n);
        this.countTimer = this.readTimer
        if(this.showTimer){
          this.startCountDownTimer()
        }
      }
    },

    mounted() {
      this.$nextTick(() => {
        this.loadPdf()
      })
      // 判断是否启动阅读计时
      this.countTimer = this.readTimer
      if(this.showTimer){
        this.startCountDownTimer()
      }
    },

    // onPageScroll(scroll) {
    //   console.log(`onPageScroll ${scroll}`,scroll);
    // },

    // onReachBottom(scroll) {
    //   console.log(`onReachBottom 滚动到底部`);
    // },

    methods: {
      handleComplete(){
        this.$emit("onClickReader")
      },
      
      startCountDownTimer(){
        this.timer= setInterval(()=>{
          // console.log(`countTimer = ${this.countTimer}`);
          if(this.countTimer<=1){
            this.btnDisabled=false
            clearInterval(this.timer)
          }else{
            --this.countTimer
          }
        },1000)
      },
      
     .........
      }
    }
  }
</script>

<style scoped>
  @import 'pdfh5/css/pdfh5.css';

  .pdf-page {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  .pdf-wrap {
    width: 100%;
    flex: 1;
  }

  .pdf-btn-group {
    width: 100%;
    border-top: 2rpx solid #F8F9FC;
    height: 8%;
    position: fixed;
    bottom: -1rpx;
    z-index: 999;
    background: white;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .btn-readed {
    background: #4E70F6;
    color: #ffffff;
    font-size: 32rpx;
  }
</style>
使用方式

主要运用在uniapp项目中

1、导入node-modules 目录中 canvas 和 pdfh5 库

2、引用组件 <u-pdf :url="pdfUrl" :show-timer="true" :disabled="disabled"></u-pdf>

pdf 预览组件 - DCloud 插件市场

相关推荐
PedroQue991 天前
uni-router v1.7.0重磅更新:守卫重定向自由掌控
前端·uni-app
一份执念3 天前
uni-app项目 (vue+vite + uni-UI)中引入umd格式JS文件,微信小程序中导入报错处理方案
前端·uni-app·echarts
PedroQue993 天前
V1.6.1性能优化:高频路径提速与代码精简
前端·uni-app
夏碧笔5 天前
uni-app跨端地图实战:用第三方LBS替代微信平台收费服务
uni-app
用户69903048487510 天前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_10 天前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app
weixin_3975740910 天前
PDF复杂表格的1:1还原引擎:跨页表格自动拼接技术实战
大数据·人工智能·pdf
Metaphor69210 天前
使用 Python 将 PDF 转换为 HTML
python·pdf·html
2601_9618451510 天前
粉笔行测5000题电子版|pdf|解析
pdf·新媒体运营·github·个人开发·内容运营·规格说明书·极限编程
Sour10 天前
PDF翻译卡住不动怎么办?扫描件、OCR 和大文件排查清单
前端·pdf·ocr