打印PDF文件流

925 阅读1分钟

传入接口地址URL

export function exportPrintGet(url) {
  fetch(url, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json;charset=utf-8'
    },
  })
    .then((res) => {
      return res.blob();
    })
    .then((res) => {
      const content = res;
      console.log(content);
      const blob = new Blob([content], { 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);
      doPrint('printPdf' + date);
      window.URL.revokeObjectURL(ifr.src); // 释放URL 对象
    })
    .catch((error) => {
      console.error(error);
    });
}

function doPrint(val) {
  var ordonnance = document.getElementById(val).contentWindow;
  setTimeout(() => {
    // window.print()
    ordonnance.print();
    this.pdfLoading = false;
  }, 100);
}