vue中预览PDF和下载PDF

vue中预览PDF和下载PDF  完整代码 

官网   GitHub - FranckFreiburger/vue-pdf: vue.js pdf viewer

首先下载插件  

npm install vue-pdf
<template> 
  <div class="content">
      <div class="pdf-tabbox" v-show="pageTotalNum">
        <div class="pdf-tab">
           <!-- <el-button type="primary" @click="prePage">上一页</el-button>
          <div>{{ pageNum }} /{{ pageTotalNum }}</div>
          <el-button type="primary" @click="nextPage">下一页</el-button> -->
          <el-button type="primary" plain @click="pdfPrintAll">
            在线打印
          </el-button>
          <el-button type="primary" @click="downLoad">下 载</el-button>
        </div>
      </div>
      <div class="pdf-boxs">
        <pdf
          class="paf-box"
          ref="pdf"
          :src="url"
          :page="pageNum"
          :rotate="pageRotate"
          @password="password"
          @progress="loadedRatio = $event"
          @page-loaded="pageLoaded($event)"
          @num-pages="pageTotalNum = $event"
          @error="pdfError($event)"
          @link-clicked="page = $event"
      >
     </pdf>
   </div>
  </div>
</template>
<script>
import pdf from "vue-pdf";
import { ticketDownload, download } from "@/api/examinee";
export default {
  data(){
    return {
      url: "", //文件地址
      pdfs: "",
      pageNum: 1,
      pageTotalNum: 1,
      pageRotate: 0,
      // 加载进度
      loadedRatio: 0,
      curPageNum: 0,
      userName: "", //考生姓名
   };           
  },
  created() {
    this.getInfo();
  },
  methods:{
    getInfo() {
      var param = {
        kcAvtId: this.$route.query.kcAvtId,
      };
      ticketDownload(param).then((res) => {
        if (res.code == 200) {
          this.url = res.data.pdfPath;
          this.pdfs = res.data.pdfName;
          this.userName = res.data.userName;
          // this.url = pdf.createLoadingTask(this.url);
          // console.log(this.url, "this.url");
        }
      });
    },
    //二进制流文件下载
    downLoad() {
      var param = {
        fileName: this.pdfs,
        delete: false,
      };
      download(param).then((res) => {
        // if (res.code == 200) {
        let pdfUrl = window.URL.createObjectURL(
          new Blob([res.data], { type: `application/pdf` })
        );
        const fname = this.userName + "的准考证.pdf"; // 下载文件的名字
        const link = document.createElement("a");
        link.href = pdfUrl;
        link.setAttribute("download", fname);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    },
    prePage() {
      var p = this.pageNum;
      p = p > 1 ? p - 1 : this.pageTotalNum;
      this.pageNum = p;
    },
    nextPage() {
      var p = this.pageNum;
      p = p < this.pageTotalNum ? p + 1 : 1;
      this.pageNum = p;
    },

    password(updatePassword, reason) {
      updatePassword(prompt('password is "123456"'));
      console.log("...reason...");
      console.log(reason);
      console.log("...reason...");
    },
    pageLoaded(e) {
      this.curPageNum = e;
    },
    pdfError(error) {
      console.error(error);
    },
    pdfPrintAll() {
      this.$refs.pdf.print();
    },
    pdfPrint() {
      this.$refs.pdf.print(100, [1, 2]);
    },
 }
}
</script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 3 预览 PDF 并隐藏下载按钮,您可以使用 PDF.js 库来加载 PDF 文件,并使用 CSS 来隐藏下载按钮。以下是一个简单的示例: 1. 安装 PDF.js 库 ``` npm install pdfjs-dist ``` 2. 在 Vue 组件引入 PDF.js 和样式文件 ```javascript import pdfjsLib from 'pdfjs-dist/webpack'; import 'pdfjs-dist/web/pdf_viewer.css'; ``` 3. 创建一个 PDF 预览的组件,使用 PDF.js 加载 PDF 文件 ```html <template> <div ref="pdfViewer"></div> </template> <script> export default { data() { return { pdfUrl: 'path/to/pdf/file.pdf', pdfViewer: null }; }, mounted() { this.loadPdf(); }, methods: { loadPdf() { const viewerContainer = this.$refs.pdfViewer; const pdfViewer = new pdfjsLib.PDFViewer({ container: viewerContainer }); viewerContainer.addEventListener('pagesinit', () => { pdfViewer.eventBus.dispatch('toggleSecondaryToolbar', { visible: false }); }); pdfjsLib.getDocument(this.pdfUrl).promise.then(pdf => { pdfViewer.setDocument(pdf); this.pdfViewer = pdfViewer; }); } } }; </script> ``` 在上面的代码,我们创建了一个 `PDFViewer` 实例,并将其添加到 Vue 组件的 DOM 。一旦 PDF 文件加载完成,我们可以将 `pdfViewer` 对象保存到组件的数据,以便在以后的操作使用。 在 `pagesinit` 事件,我们使用 PDF.js 的 `eventBus` 对象来切换 PDF 查看器的次要工具栏可见性,从而隐藏下载按钮。 最后,我们使用 `pdfjsLib.getDocument` 方法来加载 PDF 文件并将其设置到 `pdfViewer` 。 4. 在样式文件隐藏下载按钮 ```css button.toolbarButton.download { display: none !important; } ``` 在上面的样式,我们使用 `!important` 来覆盖 PDF.js 下载按钮的默认样式,并将其设置为 `display: none` 来隐藏该按钮。 这样,在 Vue 3 组件预览 PDF 时,下载按钮就会被隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值