vue-pdf 展示base64编码pdf,旋转 翻页 放大 缩小 下载

首先看一下是不是大家需要的效果
在这里插入图片描述
首先安装vue-pdf

yarn add vue-pdf

然后写一个组件

AppPdf.vue


<template>
  <div class="pdf">
    <div class="main">
      <el-row :gutter="24" style="text-align:center" v-show="pdfSrc">
        <el-button class="btn-def btn-pre" @click.stop="clock">顺时针</el-button>
        <el-button class="btn-def btn-next" @click.stop="counterClock">逆时针</el-button>
        <el-button class="btn-def btn-pre" @click.stop="prePage" :disabled="preDisable">上一页</el-button>
        <el-button class="btn-def btn-next" @click.stop="nextPage" :disabled="nextDisable">下一页</el-button>
        <el-button :class="{select:idx==0}" @touchstart="idx=0" @touchend="idx=-1" @click="scaleD">放大 </el-button>
        <el-button :class="{select:idx==1}" @touchstart="idx=1" @touchend="idx=-1" @click="scaleX">缩小</el-button>
        <el-button @click="fileDownload(pdfUrl,'pdf文件')">下载</el-button>
        <div>{{ pageNum }}/{{ pageTotalNum }}</div>
      </el-row>
      <pdf
        ref="pdf"
        :src="pdfSrc"
        :page="pageNum"
        :rotate="pageRotate"
        @password="password"
        @progress="loadedRatio = $event"
        @page-loaded="pageLoaded($event)"
        @num-pages="pageTotalNum=$event"
        @error="pdfError($event)"
        @link-clicked="page = $event">
      </pdf>
    </div>

    <el-row :gutter="24" style="text-align:center" v-show="pdfSrc">
      <div>{{ pageNum }}/{{ pageTotalNum }}</div>
      <el-button class="btn-def btn-pre" @click.stop="clock">顺时针</el-button>
      <el-button class="btn-def btn-next" @click.stop="counterClock">逆时针</el-button>
      <el-button class="btn-def btn-pre" @click.stop="prePage" :disabled="preDisable">上一页</el-button>
      <el-button class="btn-def btn-next" @click.stop="nextPage" :disabled="nextDisable">下一页</el-button>
      <el-button :class="{select:idx==0}" @touchstart="idx=0" @touchend="idx=-1" @click="scaleD">放大 </el-button>
      <el-button :class="{select:idx==1}" @touchstart="idx=1" @touchend="idx=-1" @click="scaleX">缩小</el-button>
      <el-button @click="fileDownload(pdfUrl)">下载</el-button>
    </el-row>
    <!-- <div>进度:{{ loadedRatio }}</div>
    <div>页面加载成功: {{ curPageNum }}</div> -->
  </div>
</template>
<script>
  import pdf from 'vue-pdf'
  import api from '@/api/index'
  import notification from 'ant-design-vue/es/notification'

  export default {
    name: 'AppPdf',
    components: {
      pdf
    },
    props: {
            pdfSrc: {
                type: String,
                required: true
            },
            fileName: {
                type: String,
                required: true
            },
            fid: {
                type: Number,
                required: true
            },
            loadData: {
                type: Function,
                required: true
            }
        },
    data () {
      return {
        preDisable: true,
        nextDisable: false,
        pdfUrl: '',
        pageNum: 1,
        pageTotalNum: 1,
        pageRotate: 0,
        loadedRatio: 0, // 加载进度
        curPageNum: 0,
        scale: 100, // 放大系数
        idx: -1
      }
    },
    watch: {
            pdfSrc () {
                this.curPageNum = 1
                this.pageNum = 1
            },
            fileName () {
            },
            pageNum () {
                if (this.pageNum === 1) {
                    this.preDisable = true
                } else {
                    this.preDisable = false
                }
                if (this.pageNum === this.pageTotalNum) {
                    this.nextDisable = true
                    // 请求记录接口,保存已读记录
                    this.$http
                        .post(api.ImportantFile, {
                            params: { fid: this.fid }
                        })
                        .then(res => {
                            if (res) {
                                this.$message({
                                    message: '已读 ' + this.fileName,
                                    type: 'success'
                                })
                                this.loadData()
                            }
                        })
                } else {
                    this.nextDisable = false
                }
            }
        },

    methods: {
      // 下载PDF
      fileDownload (data) {
        const filename = this.fileName || '报表.xls'
        var element = document.createElement('a')
        element.setAttribute('href', encodeURI(this.pdfSrc))
        element.setAttribute('download', 'LoginInquiry.pdf')
        element.setAttribute('download', filename)
        element.style.display = 'none'
        document.body.appendChild(element)
        element.click()
        document.body.removeChild(element)
      },
      // 放大
      scaleD () {
        this.scale += 5
        this.$refs.pdf.$el.style.width = parseInt(this.scale) + '%'
      },

      // 缩小
      scaleX () {
        if (this.scale === 100) {
          return
        }
        this.scale += -5
        this.$refs.pdf.$el.style.width = parseInt(this.scale) + '%'
      },
      prePage () {
        var p = this.pageNum
        p = p > 1 ? p - 1 : this.pageTotalNum
        this.pageNum = p
      },
      nextPage () {
        var p = this.pageNum
        p = p < this.pageTotalNum ? p + 1 : 1
        this.pageNum = p
      },
      clock () {
        this.pageRotate += 90
      },
      counterClock () {
        this.pageRotate -= 90
      },
      password (updatePassword, reason) {
        updatePassword(prompt('password is "123456"'))
      },
      pageLoaded (e) {
        this.curPageNum = e
      },
      pdfError (error) {
        console.error(error)
      },
      pdfPrintAll () {
        this.$refs.pdf.print()
      },
      pdfPrint () {
        this.$refs.pdf.print(100, [1, 2])
      }
    },
    updated () {
        // 在只有一页的时候,直接已读
        if (this.pdfSrc) {
            if (this.pageTotalNum === 1) {
                // 请求记录接口,保存已读记录
                this.$http
                    .post(api.ImportantFile, {
                        params: { fid: this.fid }
                    })
                    .then(res => {
                        if (res) {
                            notification.success({
                                message: '完成',
                                description: '已读 ' + this.fileName
                            })
                            // this.$message({
                            //     message: '已读 ' + this.fileName,
                            //     type: 'success'
                            // })
                            this.loadData()
                        }
                    })
            }
        }
    }
  }
</script>

在需要用到它的地方引入它

  import AppPdf from './AppPdf.vue'
  <app-pdf :pdfSrc="base64Str" :fileName="fileName" :fid="fid" :loadData="loadData" ref="son"></app-pdf>
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vue-pdf是一个用于在Vue.js应用程序中展示PDF文件的库。然而,vue-pdf本身并不提供双指放大缩小的功能,因为它只是用来展示和预览PDF文件的工具。 要实现双指放大缩小的功能,你需要结合Vue.js和其他库,比如Hammer.js或者Touch事件来完成。这些库可以用来监听触摸事件并识别双指缩放手势。 以下是一个简单的示例,展示如何使用Vue.js和Hammer.js来实现双指放大缩小的功能: 1. 在Vue.js组件的template中,使用vue-pdf展示PDF文件。 ```html <template> <div> <vue-pdf :src="pdfUrl" :page="pageNumber" :scale="scale"></vue-pdf> </div> </template> ``` 2. 在Vue.js组件的script中,引入Hammer.js库,并创建一个双指缩放操作的实例。 ```javascript <script> import Hammer from 'hammerjs'; export default { data() { return { pdfUrl: 'path_to_your_pdf_file.pdf', pageNumber: 1, scale: 1, }; }, mounted() { const element = this.$el.querySelector('div'); // 获取vue-pdf展示容器 const mc = new Hammer.Manager(element); const pinch = new Hammer.Pinch(); mc.add(pinch); mc.on('pinchmove', (event) => { // 获取缩放比例 const newScale = this.scale * event.scale; // 设置新的缩放比例,并更新页面 this.scale = newScale; }); }, }; </script> ``` 在上述示例中,我们使用Hammer.js来监听pinchmove事件,该事件会在双指缩放时触发。我们获取到缩放比例,并根据该比例更新vue-pdf组件的scale属性,进而实现PDF文件的放大缩小。 需要注意的是,以上只是一个简单的示例,具体的实现方式可能因应用场景的不同而有所不同。你可能需要根据自己的需求来调整代码和样式,以实现理想的双指放大缩小效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值