新窗口打印
参数:
- printRef是指打印内容最外层标签的Ref值
- 测试打印文本为打印页面的title标题
let obj = ticketRef.value
let newWindow = window.open()
// var newWindow = window.open("","_top")
let docStr = obj.innerHTML
newWindow.document.write(docStr)
newWindow.document.title = '测试打印'
newWindow.document.close()
setTimeout(function () {
newWindow.print()
// newWindow.close();
}, 10)
iframe浏览器弹窗打印
参数:
- printWindow为打印内容最外层的标签
const el = document.getElementById('winprint')
const iframe: any = document.createElement('IFRAME')
let doc = null
//让iframe结构隐藏不显示
iframe.setAttribute('style', 'position:absolute;width:0px;height:0px;left:500px;top:500px;')
document.body.appendChild(iframe)
doc = iframe.contentWindow.document
doc.write(el.innerHTML)
doc.close()
// 获取iframe的焦点,从iframe开始打印
iframe.contentWindow.focus()
iframe.contentWindow.print()