Vue使用vue-pdf实现PDF文件预览

 更新时间:2023年03月15日 14:59:41   作者:会说法语的猪  
这篇文章主要为大家详细介绍了Vue如何使用vue-pdf实现PDF文件预览的功能,文中的示例代码讲解详细,具有一定的参考价值,感兴趣的可以了解一下

前言

vue-pdf npm文档 https://www.npmjs.com/package/vue-pdf 

先看下效果 

安装

npm install vue-pdf

页面按需引入并注册

import Pdf from "vue-pdf"
export default {
  components: { Pdf }
}

完整代码

<template>
  <div class="global">
    <div class="preview">
      <el-button type="success" @click="isShow = true">开始预览</el-button>
    </div>
    <div class="pdfContainer" v-if="isShow">
      <div class="button">
        <el-button type="success" @click="prev()">上一页</el-button>
        <el-button type="success" @click="next()">下一页</el-button>
        <el-button type="success" @click="scaleD()">放大</el-button>
        <el-button type="success" @click="scaleX()">缩小</el-button>
        <el-button type="success" @click="clockwise()">顺时针</el-button>
        <el-button type="success" @click="antiClockwise()">逆时针</el-button>
      </div>
      <div class="pdf">
        <pdf
          ref="pdf"
          :src="src"
          :page="currentPage"
          :rotate="pageRotate"
          @num-pages="pageCount = $event"
          @page-loaded="currentPage = $event"
          @loaded="loadPdfHandler"
        ></pdf>
      </div>
      <div class="page">
        <span class="pageNum">{{ currentPage }}</span>
        <span class="pageNum">/</span>
        <span class="pageNum">{{ pageCount }}</span>
      </div>
    </div>
  </div>
</template>
<script>
import Pdf from "vue-pdf";
export default {
  data() {
    return {
      src: "http://storage.xuetangx.com/public_assets/xuetangx/PDF/PlayerAPI_v1.0.6.pdf",  // pdf的路径 实际项目后端返回
      currentPage: 0, // pdf文件页码
      pageCount: 0, // pdf文件总页数
      scale: 100,  //  开始的时候默认和容器一样大即宽高都是100%
      pageRotate: 0,  // 旋转角度
      isShow: false
    };
  },
  components: { Pdf },
  mounted() {
    console.log(Pdf, "Pdf----->>>");
  },
  methods: {
    // pdf加载时
    loadPdfHandler(e) {
      this.currentPage = 1; // 加载的时候先加载第一页
      this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
      this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
    },
    //  上一页
    prev() {
      if (this.currentPage > 1) {
        this.currentPage--;
      }
    },
    // 下一页
    next() {
      if (this.currentPage < this.pageCount) {
        this.currentPage++;
      }
    },
    //放大
    scaleD() {
      if(this.scale == 100) return this.$message.warning('已经是最大喽~~')
      this.scale += 10;
      this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
      this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
    },
    //缩小
    scaleX() {
      if(this.scale == 40) return this.$message.warning('已经是最小喽~~')
      this.scale += -10;
      this.$refs.pdf.$el.style.width = parseInt(this.scale) + "%";
      this.$refs.pdf.$el.style.height = parseInt(this.scale) + "%";
    },
    // 顺时针
    clockwise() {
      this.pageRotate += 90;
    },
    // 逆时针
    antiClockwise() {
      this.pageRotate -= 90;
    },
  },
};
</script>
<style scoped>
.global {
  width: 100%;
  height: 100%;
}
.preview {
  width: 100%;
  height: 50px;
}
 
.pdfContainer {
  width: 100%;
  height: calc(100% - 50px);
}
.pdfContainer .button {
  width: 100%;
  height: 50px;
  display: flex;
  align-items: center;
}
.pdfContainer .pdf {
  width: 100%;
  height: calc(100% - 100px);
  overflow-y: auto;
}
.pdfContainer .page {
  width: 100%;
  height: 50px;
}
 
.pageNum {
  font-size: 28px;
}
</style>

到此这篇关于Vue使用vue-pdf实现PDF文件预览的文章就介绍到这了,更多相关Vue PDF文件预览内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • vue+Echart实现立体柱状图

    vue+Echart实现立体柱状图

    这篇文章主要为大家详细介绍了vue+Echart实现立体柱状图,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-04-04
  • 在Vue项目中集成和使用Lottie动画库的步骤详解

    在Vue项目中集成和使用Lottie动画库的步骤详解

    Lottie 是一个由 Airbnb 开源的动画库,它允许你在 Web、iOS、Android 等平台上使用体积小、高性能的体验丰富的矢量动画,本文将详细介绍在 Vue 项目中如何集成和使用 Lottie,文中有详细的代码讲解,需要的朋友可以参考下
    2023-11-11
  • vue中使用iframe嵌入网页,页面可自适应问题

    vue中使用iframe嵌入网页,页面可自适应问题

    这篇文章主要介绍了vue中使用iframe嵌入网页,页面可自适应问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-09-09
  • vue写h5页面的方法总结

    vue写h5页面的方法总结

    在本篇内容里小编给大家整理了关于vue写h5页面的方法以及注意点分析,有需要的朋友们跟着学习下吧。
    2019-02-02
  • vue 数据遍历筛选 过滤 排序的应用操作

    vue 数据遍历筛选 过滤 排序的应用操作

    这篇文章主要介绍了vue 数据遍历筛选 过滤 排序的应用操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-11-11
  • vue中jsencrypt与base64加密解密的实用流程

    vue中jsencrypt与base64加密解密的实用流程

    vue项目里面使用到的加密和解密的方法,本文主要介绍了vue中jsencrypt与base64加密解密的实用流程,具有一定的参考价值,感兴趣的可以了解一下
    2023-10-10
  • vue之el-upload使用FormData多文件同时上传问题

    vue之el-upload使用FormData多文件同时上传问题

    这篇文章主要介绍了vue之el-upload使用FormData多文件同时上传问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-05-05
  • 重新认识vue之事件阻止冒泡的实现

    重新认识vue之事件阻止冒泡的实现

    这篇文章主要介绍了重新认识vue之事件阻止冒泡的实现,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • Vue路由跳转与接收参数的实现方式

    Vue路由跳转与接收参数的实现方式

    这篇文章主要介绍了Vue路由跳转与接收参数的实现方式,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-04-04
  • Vue组件间的通信pubsub-js实现步骤解析

    Vue组件间的通信pubsub-js实现步骤解析

    这篇文章主要介绍了Vue组件间的通信pubsub-js实现原理解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-03-03

最新评论