vue 打印,blob两种方式
2023-10-26 本文已影响0人
我是七月
方式一
const ifr = document.createElement('iframe');
const date = new Date().getTime();
ifr.style.frameborder = 'no';
ifr.style.display = 'none';
ifr.style.pageBreakBefore = 'always';
ifr.setAttribute('id', 'printPdf' + date);
ifr.setAttribute('name', 'printPdf' + date);
ifr.src = event.data.data;
document.body.appendChild(ifr);
const printIframe = document.getElementById('printPdf' + date);
setTimeout(() => {
printIframe.contentWindow?.print();
}, 1000);
window.URL.revokeObjectURL(ifr.src); // 释放URL 对象
其中 ifr.src
是就是流文件地址,格式为
blob:http://10.0.8.94:81/1dc58787-745d-42b9-87b5-559c5150e8c5
方式二
<iframe id="printIframe" hidden height="{0}"></iframe>
const printIframe = document.getElementById('printIframe');
printIframe.src = event.data.data;
setTimeout(() => {
printIframe.contentWindow?.print();
}, 1000);