【VUE 获取PDF文档流直接打印】
- 2024-06-27 10:22
- pdf, 前端, vue.js, javascript, ecmascript
- 50人 已看
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)
}