当前位置:   article > 正文

【VUE 获取PDF文档流直接打印】_前端 拿到文件流直接打印

前端 拿到文件流直接打印
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(() => {})
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

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)

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/天景科技苑/article/detail/880357
推荐阅读
相关标签
  

闽ICP备14008679号