iframe显示pdf文件并打印

552 阅读1分钟

juejin.cn/post/684490… 其中一步参考了这个文章才渲染出来内容. 以及中间的跨域解决.感谢老哥. 不过我的实现目的有些不同吧. 也参考了其他别的一些博客

const printUrl = 'http...'
const printFrame = document.createElement('iframe');
printFrame.setAttribute('style', 'display:none');
fetch(printUrl)
  .then(res => res.blob()) // 解析res值为blob
  .then(response => {
    const url = URL.createObjectURL(response);
    printFrame.src = url
  })
document.body.appendChild(printFrame);
printFrame.onload = () => {
//调用这步来实现 iframe窗口中的打印
  printFrame.contentWindow.print();
};

这样就召唤出浏览器中默认的打印了.