From bf88e76e040737c5d426a75155e24b2c97c92e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BA=91=E9=A3=9E?= Date: Fri, 18 Nov 2022 14:38:25 +0800 Subject: [PATCH] fix:some environment browsers cannot print --- src/editor/utils/print.ts | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/editor/utils/print.ts b/src/editor/utils/print.ts index d9f49ad..7522890 100644 --- a/src/editor/utils/print.ts +++ b/src/editor/utils/print.ts @@ -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 + }) }) } \ No newline at end of file