vue-pdf 预览和下载

本文介绍三种不同的PDF处理方式:极简模式预览PDF,下载模式直接下载PDF为ZIP,及文件流模式显示PDF。通过不同场景下的实现代码示例,帮助读者理解如何在Web应用中集成这些功能。
  极简模式:pdf 预览


   handelDownload(row) {
      this.$axios({
        method: "get",
        url: baseUrl + getUrl,
        responseType: "arraybuffer",
        header: { "Content-Type": "multipart/form-data" },
        params: {
          billId: row.id,
        },
      }).then((resp) => {
        const binaryData = [];
         binaryData.push(resp.data);
        //获取blob链接
        this.pdfUrl = window.URL.createObjectURL(new Blob(binaryData, { type: 'application/pdf' }));
        window.open(this.pdfUrl);
      });
    },

  下载模式:pdf 下载

    handelDownload(row) {
      request({
        url: baseUrl + getUrl,
        method: "put",
        responseType: "blob",
        header: { "Content-Type": "multipart/form-data" },
        data: {
          idList: this.ids,
        },
      })
        .then((res) => {
          const url = window.URL.createObjectURL(new Blob([res.data]));
          const link = window.document.createElement("a");
          link.style.display = "none";
          link.href = url;
          link.setAttribute("download", "pdf" + ".zip");
          document.body.appendChild(link);
          link.click();
        })
        .catch((res) => {
        });
    },



 文件流模式:
  this.$axios({
        method: "get",
        url: baseUrl + getUrl,
        responseType: "arraybuffer",
        header: { "Content-Type": "multipart/form-data" },
        params: {
          billId: row.id,
        },
      }).then((resp) => {
        const binaryData = [];
        binaryData.push(resp.data);
        //获取blob链接
        this.pdfUrl = window.URL.createObjectURL(
          new Blob(binaryData, { type: "application/pdf" })
        );
        this.keyValue = row.id;
        this.flag = true;
      });

     <iframe :src="pdfUrl" page="0" frameborder="0" style="width: 100%; height: 100%"></iframe>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值