fix:some environment browsers cannot print

pr675
黄云飞 3 years ago
parent bbd3914b44
commit bf88e76e04

@ -1,7 +1,16 @@
export function printImageBase64(base64List: string[], width: number, height: number) {
const iframe = document.createElement('iframe')
// 离屏渲染
iframe.style.visibility = 'hidden'
iframe.style.position = 'absolute'
iframe.style.left = '0'
iframe.style.top = '0'
iframe.style.width = '0'
iframe.style.height = '0'
iframe.style.border = 'none'
document.body.append(iframe)
const doc = iframe.contentWindow!.document
const contentWindow = iframe.contentWindow!
const doc = contentWindow.document
doc.open()
const container = document.createElement('div')
base64List.forEach(base64 => {
@ -16,8 +25,13 @@ export function printImageBase64(base64List: string[], width: number, height: nu
style.append(document.createTextNode(stylesheet))
setTimeout(() => {
doc.write(`${style.outerHTML}${container.innerHTML}`)
iframe.contentWindow?.print()
contentWindow.print()
doc.close()
iframe.remove()
// 移除iframe
window.addEventListener('mouseover', () => {
iframe?.remove()
}, {
once: true
})
})
}
Loading…
Cancel
Save