pdfView.vue 4.42 KB
<template>
  <div class="global">
    <!-- <div class="preview">
      <a-button type="primary" @click="isShow = true">开始预览</a-button>
    </div> -->
    <div class="pdfContainer" v-if="isShow">
      <div class="button">
        <a-button type="primary" size="small" @click="prev()">上一页</a-button>
        <a-button type="primary" size="small" style="margin-left: 5px;" @click="next()">下一页</a-button>
        <a-button type="primary" size="small" style="margin-left: 5px;" @click="scaleD()">放大</a-button>
        <a-button type="primary" size="small" style="margin-left: 5px;" @click="scaleX()">缩小</a-button>
        <a-button type="primary" size="small" style="margin-left: 5px;" @click="clockwise()">顺时针</a-button>
        <a-button type="primary" size="small" style="margin-left: 5px;" @click="antiClockwise()">逆时针</a-button>
        <a-button type="primary" size="small" style="margin-left: 5px;" @click="FileDownload()" icon="download">下载</a-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'
import axios from 'axios'
//import CMapReaderFactory from 'vue-pdf/src/CMapReaderFactory.js'

export default {
  name: 'pdfView',
  data() {
    return {
      src: '',  // pdf的路径 实际项目后端返回
      currentPage: 0, // pdf文件页码
      pageCount: 0, // pdf文件总页数
      scale: 100,  //  开始的时候默认和容器一样大即宽高都是100%
      pageRotate: 0,  // 旋转角度
      isShow: true
    };
  },
  props: {
    value: {
      type: String,
      default() {
        return null
      },
    },
    fileName: {
      type: String,
      default() {
        return null
      }
    }
  },
  components: { Pdf },
  created() {
    this.src = Pdf.createLoadingTask({ url: this.value, cMapUrl: 'https://unpkg.com/pdfjs-dist@2.0.943/cmaps/', cMapPacked: true })//this.value
  },
  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;
    },
    FileDownload() {
      axios({
        url: this.value, // 替换为实际文件的URL
        method: 'GET',
        responseType: 'blob' // 告诉axios返回的数据类型为Blob
      }).then(response => {
        const url = window.URL.createObjectURL(new Blob([response.data]))
        const link = document.createElement('a')
        link.href = url
        link.setAttribute('download', this.fileName) // 下载文件的名称
        document.body.appendChild(link)
        link.click()
      })
    }
  },
};
</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: 15px;
}
</style>