From c6534f766d8640cbea4e441541065d25e0dd8b82 Mon Sep 17 00:00:00 2001 From: Hufe921 Date: Fri, 8 Mar 2024 20:33:17 +0800 Subject: [PATCH] feat: set print layout format when printing #448 --- src/editor/core/command/CommandAdapt.ts | 8 ++++++-- src/editor/utils/print.ts | 21 ++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 445b87f..5ae1a65 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -1920,7 +1920,7 @@ export class CommandAdapt { } public async print() { - const { scale, printPixelRatio } = this.options + const { scale, printPixelRatio, paperDirection } = this.options if (scale !== 1) { this.draw.setPageScale(1) } @@ -1930,7 +1930,11 @@ export class CommandAdapt { pixelRatio: printPixelRatio, mode: EditorMode.PRINT }) - printImageBase64(base64List, width, height) + printImageBase64(base64List, { + width, + height, + direction: paperDirection + }) if (scale !== 1) { this.draw.setPageScale(scale) } diff --git a/src/editor/utils/print.ts b/src/editor/utils/print.ts index eda21ad..b7860b5 100644 --- a/src/editor/utils/print.ts +++ b/src/editor/utils/print.ts @@ -1,8 +1,15 @@ +import { PaperDirection } from '../dataset/enum/Editor' + +export interface IPrintImageBase64Option { + width: number + height: number + direction?: PaperDirection +} export function printImageBase64( base64List: string[], - width: number, - height: number + options: IPrintImageBase64Option ) { + const { width, height, direction = PaperDirection.VERTICAL } = options const iframe = document.createElement('iframe') // 离屏渲染 iframe.style.visibility = 'hidden' @@ -25,7 +32,15 @@ export function printImageBase64( container.append(image) }) const style = document.createElement('style') - const stylesheet = `*{margin:0;padding:0;}@page{margin:0;}` + const stylesheet = ` + * { + margin: 0; + padding: 0; + } + @page { + margin: 0; + size: ${direction === PaperDirection.HORIZONTAL ? `landscape` : `portrait`}; + }` style.append(document.createTextNode(stylesheet)) setTimeout(() => { doc.write(`${style.outerHTML}${container.innerHTML}`)