当前位置: 首页 > news >正文

vueshowpdf 移动端pdf文件预览

1、安装

npm install vueshowpdf -S

2、参数

属性说明类型默认值
v-model是否显示pdf--
pdfurlpdf的文件地址String-

scale

默认放大倍数

Number1.2

minscale

最小放大倍数

Number0.8

maxscale

最大放大倍数

Number2

3、事件

名称说明回调参数
closepdf pdf关闭事件-
pdferr文件地址解析错误事件-

4、核心代码

<template><div class="case-info"><van-cell title="附件" value="预览" is-link @click="viewPdf" class="perview" /><vueshowpdf class="pdf" v-model="isShowPdf" :pdfurl="pdfurl":minscale='0.4' :scale='0.6' @closepdf="isShowPdf = false" @pdferr="pdfError"></vueshowpdf></div>
</template><script>
import api from '@/api.js'
import vueshowpdf from 'vueshowpdf'
export default {name: 'test',data () {return {isShowPdf: false,pdfUrl: '',pdfKey: 0}},components: {vueshowpdf},deactivated () {// 离开页面默认关闭pdfthis.isShowPdf = falsethis.pdfKey++},methods: {// 预览pdfasync viewPdf () {if (this.pdfUrl) {this.isShowPdf = true} else {this.$toast.loading({message: '加载中...',duration: 0,forbidClick: true})try {const res = await api.getFilePreview()this.$toast.clear()if (res.code === '0') {let blob = this.dataURLtoBlob(res.data)this.pdfurl = URL.createObjectURL(blob)this.isShowPdf = true} else {this.$toast(res.msg || '该原始文件不存在')}} catch (error) {this.$toast.clear()}}},// 将base64转换为文件dataURLtoBlob (dataurl) {const bstr = atob(dataurl)let n = bstr.lengthconst u8arr = new Uint8Array(n)while (n--) {u8arr[n] = bstr.charCodeAt(n)}return new Blob([u8arr], { type: 'application/pdf' })},// 关闭pdfclosePdf () {this.isShowPdf = falsethis.pdfKey++},// pdf预览失败pdfError (errorUrl) {this.$toast.fail('预览失败')this.isShowPdf = falsethis.pdfKey++}}
}
</script><style lang="scss" scoped>.perview .van-cell__value {color: #468bff;text-decoration: underline;}
</style>

http://www.lryc.cn/news/167188.html

相关文章:

  • C#根据excel文件中的表头创建数据库表
  • js通过xpath定位元素并且操作元素以下拉框select为例
  • 数据类型
  • vue 模板应用
  • Golang教程与Gin教程合集,入门到实战
  • 国家网络安全周 | 天空卫士荣获“2023网络安全优秀创新成果大赛优胜奖”
  • Swift学习笔记一(Array篇)
  • C++项目实战——基于多设计模式下的同步异步日志系统-②-前置知识补充-不定参函数
  • C++使用Boost库加入UDP组播时程序崩溃
  • 华为HCIA(四)
  • Qt --- Day01
  • 24.98万起,新一代AITO问界M7值得买吗?
  • Java毕业设计 SSM SpringBoot 水果蔬菜商城
  • 前端JS中的异步编程与Promise
  • Pytorch Advanced(二) Variational Auto-Encoder
  • Flask 使用 JWT(三)flask-jwt-extended
  • 堆与栈的区别
  • OpenWrt kernel install分析(2)
  • 【计算机网络】传输层协议——TCP(下)
  • Vue前端页面打印
  • Visual Studio将C#项目编译成EXE可执行程序
  • git把某一次commit修改过的文件打包导出(git)
  • Vue3 Ajax(axios)异步
  • idea2023全量方法debug
  • Docker镜像解析获取Dockerfile文件
  • 使用maven命令打jar包
  • 【多线程】死锁 详解
  • 成考[专升本政治]科目必背知识点
  • spring boot 使用AOP+自定义注解+反射实现操作日志记录修改前数据和修改后对比数据,并保存至日志表
  • 【深度学习】Pytorch 系列教程(二):PyTorch数据结构:1、Tensor(张量): GPU加速(GPU Acceleration)