vue-pdf使用

<template>
  <div class="pdf-preview">
    <pdf ref="pdf" :src="src" :page="pageNum" @page-loaded="pageLoaded($event)" @num-pages="pageTotalNum = $event" @error="pdfError($event)" @progress="onProgress">
    </pdf>
    <div class="tools" v-if="show">
      <div class="fenye">
        <div @click="prePage" class="mr10"> 上一页</div>
        <div @click="nextPage" class="mr10"> 下一页</div>
        <span class="page">{{ pageNum }}/{{ pageTotalNum }}</span>
      </div>
    </div>
  </div>
</template>
<script>
import pdf from "vue-pdf";
export default {
  name: "pdfPreview",
  props: {
    url: {
      default: "",
      type: String,
    },
  },
  components: {
    pdf,
  },
  data() {
    return {
      src: "/static/xxx.pdf", // 预览地址
      pageNum: 1, // 当前页码
      pageTotalNum: 1, // 总页数
      Num: 0,
      NumA: 0, //总个数
      Numnow: 1, //当前个数

      show: false,
    };
  },
  created() {
    console.log("this:", this);
    this.$toast.loading().hide();
  },
  mounted() {
    this.$toast.loading("加载中...");

    // 有时PDF文件地址会出现跨域的情况,这里最好处理一下
    this.src = pdf.createLoadingTask(this.src);
  },
  methods: {
    // 上一页函数,
    prePage() {
      if (this.pageNum < this.pageTotalNum) {
        return;
      }
      this.$toast.loading("加载中...");
      var page = this.pageNum;
      page = page > 1 ? page - 1 : this.pageTotalNum;
      this.pageNum = page;
    },
    // 下一页函数
    nextPage() {
      if (this.pageNum >= this.pageTotalNum) {
        return;
      }
      this.$toast.loading("加载中...");
      var page = this.pageNum;
      page = page < this.pageTotalNum ? page + 1 : 1;
      this.pageNum = page;
    },
    // 页面加载回调函数,其中e为当前页数
    pageLoaded(e) {
      console.log({ e });
      this.curPageNum = e;
      setTimeout(() => {
        this.$toast.loading().hide();
      }, 1000);
    },
    onProgress(status) {
      if (status == 1) {
        this.show = true;
        this.$toast.loading().hide();
      }
    },

    // 抛出错误的回调函数。
    pdfError(error) {
      console.error(error);
    },
  },
  beforeDestroy() {
    this.$toast.loading().hide();
  },
};
</script>
<style lang="scss" scoped>
.pdf-preview {
  padding: 12px;
  .head {
    margin-bottom: 10px;
    display: flex;
    justify-content: space-between;
  }
  .tools {
    position: fixed;
    left: 20px;
    bottom: 70px;
    .fenye {
      margin-top: 20px;
      font-size: 12px;
      display: flex;
      //   justify-content: space-between;
      align-items: center;
      .mr10 {
        background-color: #eee;
        padding: 3px 15px;
        border-radius: 4px;
        margin-right: 20px;
      }
    }
  }
  .page {
    margin-left: 10px;
  }
}
</style>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值