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