个人技术分享

const params = {
        id: this.base_info.id,
        is_export: 1
      }
      http
        .download('post', api.exportPdfByPhp, params)
        .then(response => {
          let res =
              response.headers['content-disposition'].match(
                /filename="(\S*)"/
              )
          if (response.config.responseType === 'blob') {
              printFile(response.data,res && decodeURIComponent(res[1]))
          }
        })
        .catch(() => {})

res 返回值

请添加图片描述

export function printFile(data,filename = "模板") {
    let file = new File([data], filename, blob);
  const blob = new Blob([file], { 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)
  var ordonnance = document.getElementById('printPdf' + date).contentWindow
  setTimeout(() => {
    ordonnance.print()
  }, 100)
  window.URL.revokeObjectURL(ifr.src) // 释放URL 对象
  console.log(ifr.src)

}