帮某个驾校做的打印系统,设计到web打印,故记录一下解决方案:
iframePrint(){
try{
const el = document.getElementById("preview-model");
const iframe = document.createElement('IFRAME');
var doc;
iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:-10px;top:-10px;');
document.body.appendChild(iframe);
doc = iframe.contentWindow.document;
doc.write('<div>' + el.innerHTML + '</div>');
doc.close();
iframe.contentWindow.focus();
iframe.contentWindow.print();
setTimeout(() => {
document.body.removeChild(iframe);
},300);
}
catch(err){
console.error('Print error')
}
}
网友评论