在线预览
1.请求接口 请求设置responseType
axios.get(url,{resonseType:'blob'})
2.根据返回的值创建一个Blob对象,
// 以pdf文件为例
let blob = new Blob([result],{
type: "application/pdf;chartset=UTF-8"
})
3.window.URL.createObjectURL创建一个url连接
let blob = new Blob([result],{
type: "application/pdf;chartset=UTF-8"
})
let url = window.URL.createObjectURL(blob)
4.在线预览
可以用iframe预览,url的值为 window.URL.createObjectURL(blob)
,
或者直接打开window.open(url)
打印
1.创建iframe标签
const iframe = document.createElement('iframe')
2.属性相关
iframe.className = 'tmp-pdf';
iframe.style.display = 'none'
// blobUrl 像在线预览1,2,3这样得来的url
iframe.src = blobUrl
3.创建好的iframe添加到页面body
document.body.appendChild(iframe)
4.执行打印方法,并移除iframe
setTimeout(function() {
iframe.contentWindow.print()
document.body.removechild(iframe)
}, 100)
网友评论