实现文件打印(pdf)

2022-04-15  本文已影响0人  PharkiLL
        
        const content = res.data(接口返回的数据)
        console.log(content)
        // 转换必须要加上{ type: 'application/pdf' },不然无法进行打印
        const blob = new Blob([content], { type: 'application/pdf' })
        let date = (new Date()).getTime()
        let 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)
        setTimeout(() => {
           let ordonnance = document.getElementById('printPdf' + date).contentWindow
            setTimeout(() => {
               ordonnance.print()
            }, 100)
          window.URL.revokeObjectURL(ifr.src) // 释放URL 对象
        }, 1000);
上一篇下一篇

猜你喜欢

热点阅读