最新公告
  • 欢迎您光临起源地模板网,本站秉承服务宗旨 履行“站长”责任,销售只是起点 服务永无止境!立即加入钻石VIP
  • vue-pdf 展示base64编码pdf,旋转 翻页 放大 缩小 下载

    正文概述 掘金(dangsh)   2021-07-28   1320

    首先看一下是不是大家需要的效果 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>
    

    起源地下载网 » vue-pdf 展示base64编码pdf,旋转 翻页 放大 缩小 下载

    常见问题FAQ

    免费下载或者VIP会员专享资源能否直接商用?
    本站所有资源版权均属于原作者所有,这里所提供资源均只能用于参考学习用,请勿直接商用。若由于商用引起版权纠纷,一切责任均由使用者承担。更多说明请参考 VIP介绍。
    提示下载完但解压或打开不了?
    最常见的情况是下载不完整: 可对比下载完压缩包的与网盘上的容量,若小于网盘提示的容量则是这个原因。这是浏览器下载的bug,建议用百度网盘软件或迅雷下载。若排除这种情况,可在对应资源底部留言,或 联络我们.。
    找不到素材资源介绍文章里的示例图片?
    对于PPT,KEY,Mockups,APP,网页模版等类型的素材,文章内用于介绍的图片通常并不包含在对应可供下载素材包内。这些相关商业图片需另外购买,且本站不负责(也没有办法)找到出处。 同样地一些字体文件也是这种情况,但部分素材会在素材包内有一份字体下载链接清单。
    模板不会安装或需要功能定制以及二次开发?
    请QQ联系我们

    发表评论

    还没有评论,快来抢沙发吧!

    如需帝国cms功能定制以及二次开发请联系我们

    联系作者

    请选择支付方式

    ×
    迅虎支付宝
    迅虎微信
    支付宝当面付
    余额支付
    ×
    微信扫码支付 0 元