PDF打印

81 阅读1分钟

//后台返回的必须是pdf格式

    scatteredPurchaseTable(id) {
return request({
  url: `/business/material-purchase-order/scatteredPurchaseTable?id=${id}`,
  method: 'get',
  responseType: 'blob' //注意加上这个blob
})
  },

  scatteredPurchaseTable(id) {
  this.$api.scatteredPurchaseTable(id).then((res) => {
    if (typeof window.navigator.msSaveBlob !== "undefined") {
      window.navigator.msSaveBlob(new Blob([res]), "零散采购单" + ".docx");
    } else {
      // type:application/octet-stream
      const blob = new Blob([res], { type: "application/pdf" });
      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 = window.URL.createObjectURL(blob);
      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();
  }, 500);
},