【vue-pdf】简单封装pdf预览组件

【vue-pdf】简单封装pdf预览组件

在Vue中使用vue-pdf来展示PDF文件,首先需要安装vue-pdf:

js 复制代码
npm i vue-pdf

或者

js 复制代码
yarn add vue-pdf

然后在Vue组件中引入并使用vue-pdf:

js 复制代码
/**
* @描述: pdf预览组件
* @作者: xingyue
* @创建时间: 2024-11-05 14:27:19
*/
<template>
  <a-layout>
    <a-layout-header class="header">
      <div class="flex justify-between">
        <div>
          <a-button @click.stop="clock" class="xf-margin-right-sm">顺时针</a-button>
          <a-button @click.stop="counterClock" class="xf-margin-right-sm">逆时针</a-button>
          <a-button @click="$refs.pdf.print()" icon="printer" class="xf-margin-right-sm">打印</a-button>
          <!--          <a-button icon="plus" @click="enlarge">放大</a-button>-->
          <!--          <a-button icon="minus" @click="reduce">缩小</a-button>-->
        </div>

        <div>
          <a-button @click.stop="prePage">上一页</a-button>
          <span class="text-white xf-margin-lr">{{ pageNum }}/{{ pageTotalNum }}</span>
          <a-button @click.stop="nextPage">下一页</a-button>
        </div>
      </div>
    </a-layout-header>

    <a-layout-content style="max-height: calc(100vh - 275px);overflow: auto;">
      <pdf ref="pdf"
           :src="pdfUrl"
           :page="pageNum"
           :rotate="pageRotate"
           @progress="loadedRatio = $event"
           @page-loaded="pageLoaded($event)"
           @num-pages="pageTotalNum=$event"
           @error="pdfError($event)"
           @link-clicked="page = $event"></pdf>
    </a-layout-content>
  </a-layout>
</template>

<script lang="ts">
import {
  Vue, Component, Prop, Watch,
} from 'vue-property-decorator';

import pdf from 'vue-pdf';

@Component({
  name: 'GPdfPreview',
  components: { pdf },
})
export default class GPdfPreview extends Vue {
  @Prop({
    type: String,
    default: '',
  }) pdfUrl: string | undefined;

  pageNum = 1;

  pageTotalNum = 1;

  pageRotate = 0;

  // 加载进度 d
  loadedRatio = 0;

  curPageNum = 0;

  // 缩放系数
  // scale = 100;

  prePage() {
    let p = this.pageNum;
    p = p > 1 ? p - 1 : this.pageTotalNum;
    this.pageNum = p;
  }

  nextPage() {
    let p = this.pageNum;
    p = p < this.pageTotalNum ? p + 1 : 1;
    this.pageNum = p;
  }

  clock() {
    this.pageRotate += 90;
  }

  counterClock() {
    this.pageRotate -= 90;
  }

  pageLoaded(e) {
    this.curPageNum = e;
  }

  pdfError(error) {
    console.error(error);
  }

  // // 放大
  // enlarge() {
  //   this.scale += 5;
  //   // this.$refs.wrapper.$el.style.transform = "scale(" + this.scale + ")";
  //   debugger;
  //   this.$refs.pdf.$el.style.width = `${parseInt(this.scale)}%`;
  // }
  //
  // // 缩小
  // reduce() {
  //   if (this.scale == 100) {
  //     return;
  //   }
  //   this.scale += -5;
  //   this.$refs.pdf.$el.style.width = `${parseInt(this.scale)}%`;
  // }
}
</script>

<style lang="less">

</style>
相关推荐
xiaoqi92216 分钟前
React Native鸿蒙跨平台如何实现分类页面组件通过searchQuery状态变量管理搜索输入,实现了分类的实时过滤功能
javascript·react native·react.js·ecmascript·harmonyos
qq_1777673739 分钟前
React Native鸿蒙跨平台实现应用介绍页,实现了应用信息卡片展示、特色功能网格布局、权限/联系信息陈列、评分展示、模态框详情交互等通用场景
javascript·react native·react.js·ecmascript·交互·harmonyos
2603_949462101 小时前
Flutter for OpenHarmony社团管理App实战:预算管理实现
android·javascript·flutter
wuhen_n1 小时前
JavaScript内存管理与执行上下文
前端·javascript
Hi_kenyon1 小时前
理解vue中的ref
前端·javascript·vue.js
jin1233222 小时前
基于React Native鸿蒙跨平台地址管理是许多电商、外卖、物流等应用的重要功能模块,实现了地址的添加、编辑、删除和设置默认等功能
javascript·react native·react.js·ecmascript·harmonyos
2501_920931702 小时前
React Native鸿蒙跨平台医疗健康类的血压记录,包括收缩压、舒张压、心率、日期、时间、备注和状态
javascript·react native·react.js·ecmascript·harmonyos
橙露3 小时前
React Hooks 深度解析:从基础使用到自定义 Hooks 的封装技巧
javascript·react.js·ecmascript
2501_920931704 小时前
React Native鸿蒙跨平台使用useState管理健康记录和过滤状态,支持多种健康数据类型(血压、体重等)并实现按类型过滤功能
javascript·react native·react.js·ecmascript·harmonyos
Ulyanov4 小时前
从静态到沉浸:打造惊艳的Web技术发展历程3D时间轴
前端·javascript·html5·gui开发