vue接受后台文件流进行打印

请求类型 responseType: "blob",
axios({
          methods: "get",
          url: url,
          params: {
            contractId: this.contractId,
          },
          responseType: "blob",
        }).then((res) => {
          const content = res.data;
          this.pdfUrl = window.URL.createObjectURL(
            new Blob([content], { type: "application/pdf" })
          );
          // window.open(this.pdfUrl);
          var date = new Date().getTime();
          var ifr = document.createElement("iframe");
          ifr.style.frameborder = "no";
          ifr.style.display = "none";
          ifr.style.pageBreakBefore = "always";
          ifr.setAttribute("id", "printPdf" + date);
          ifr.setAttribute("name", "printPdf" + date);
          ifr.src = this.pdfUrl;
          document.body.appendChild(ifr);
          this.doPrint("printPdf" + date);
          window.URL.revokeObjectURL(ifr.src); // 释放URL 对象
        });
//打印
    doPrint(val) {
      var ordonnance = document.getElementById(val).contentWindow;
      setTimeout(() => {
        ordonnance.print();
        this.pdfLoading = false;
      }, 100);
    },
posted @ 2022-08-31 17:08  seekHelp  阅读(1173)  评论(0编辑  收藏  举报