vue-pdf

写在前面:
整理下 本地跑项目可能出现跨域问题无法看到效果 简单就直接提交到线上去 到线上就好了

1.简单使用vue-pdf 无翻页
引用: npm install --save vue-pdf

<template>
  <div>
   <pdf :src="benyeshuju.linkUrl"></pdf>
    <a :href="benyeshuju.linkUrl">
      <van-button class="btn" type="primary">
      	下载资料
      </van-button>
    </a>
  </div>
</template>

<script>
import axios from "axios";
import pdf from 'vue-pdf'
export default {
  components: { pdf },
  data() {
    return {
      id: "",
      benyeshuju: "",
    };
  },
  created() {   //created 里面就是从服务器获取pdf的地址 根据自己的需求去改
    axios
      .post("/api/resource/detail ", {
        id: this.id,
      })
      .then((res) => {
        this.benyeshuju = res.data.data;  
      });
  },
  methods: {},
};
</script>

2 复杂使用

<template>
  <div>
    <pdf
      :src="src"
      :page="currentPage"
      @num-pages="pageCount = $event"
      @page-loaded="currentPage = $event"
      @loaded="loadPdfHandler"
    >
    </pdf>
    <p class="arrow">
      <!-- // 上一页 -->
      <span @click="changePdfPage(0)" class="turn" :class="{ grey: currentPage == 1 }"
        >上一页</span
      >
      {{ currentPage }} / {{ pageCount }}
      <!-- // 下一页 -->
      <span
        @click="changePdfPage(1)"
        class="turn"
        :class="{ grey: currentPage == pageCount }"
        >下一页</span
      >
    </p>
    <a :href="benyeshuju.linkUrl">
      <van-button class="btn" @click="btn_duihuan" type="primary">下载资料</van-button>
    </a>
  </div>
</template>

<script>
import axios from "axios";
import pdf from "vue-pdf";
export default {
  components: { pdf },
  data() {
    return {
      id: "",
      benyeshuju: "",
      currentPage: 0, // pdf文件页码
      pageCount: 0, // pdf文件总页数
      fileType: "pdf", // 文件类型
      src: "", // pdf文件地址  不用动下面赋值
    };
  },
  created() { //created 里面就是从服务器获取pdf的地址 根据自己的需求去改
    this.id = this.$route.query.id;
    axios
      .post(" /api/resource/detail ", {
        id: this.id,
      })
      .then((res) => {
        this.benyeshuju = res.data.data;
        this.src = pdf.createLoadingTask(this.benyeshuju.linkUrl); 
        //防止本地跨域  但是好像没有用  
      });
  },
  methods: {
    changePdfPage(val) {
      // console.log(val)
      if (val === 0 && this.currentPage > 1) {
        this.currentPage--;
        // console.log(this.currentPage)
      }
      if (val === 1 && this.currentPage < this.pageCount) {
        this.currentPage++;
        // console.log(this.currentPage)
      }
    },
    // pdf加载时
    loadPdfHandler(e) {
      this.currentPage = 1; // 加载的时候先加载第一页
    },
  },
};
</script>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值