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) { export function printImageBase64(base64List: string[], width: number, height: number) {
const iframe = document.createElement('iframe') 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) document.body.append(iframe)
const doc = iframe.contentWindow!.document const contentWindow = iframe.contentWindow!
const doc = contentWindow.document
doc.open() doc.open()
const container = document.createElement('div') const container = document.createElement('div')
base64List.forEach(base64 => { base64List.forEach(base64 => {
@ -16,8 +25,13 @@ export function printImageBase64(base64List: string[], width: number, height: nu
style.append(document.createTextNode(stylesheet)) style.append(document.createTextNode(stylesheet))
setTimeout(() => { setTimeout(() => {
doc.write(`${style.outerHTML}${container.innerHTML}`) doc.write(`${style.outerHTML}${container.innerHTML}`)
iframe.contentWindow?.print() contentWindow.print()
doc.close() doc.close()
iframe.remove() // 移除iframe
window.addEventListener('mouseover', () => {
iframe?.remove()
}, {
once: true
})
}) })
} }
Loading…
Cancel
Save