diff --git a/src/editor/core/command/CommandAdapt.ts b/src/editor/core/command/CommandAdapt.ts index 000baed..3cd51d8 100644 --- a/src/editor/core/command/CommandAdapt.ts +++ b/src/editor/core/command/CommandAdapt.ts @@ -1948,12 +1948,11 @@ export class CommandAdapt { } public async print() { - const { scale, printPixelRatio, paperDirection } = this.options + const { scale, printPixelRatio, paperDirection, width, height } = + this.options if (scale !== 1) { this.draw.setPageScale(1) } - const width = this.draw.getOriginalWidth() - const height = this.draw.getOriginalHeight() const base64List = await this.draw.getDataURL({ pixelRatio: printPixelRatio, mode: EditorMode.PRINT diff --git a/src/editor/utils/print.ts b/src/editor/utils/print.ts index b7860b5..7a9ef04 100644 --- a/src/editor/utils/print.ts +++ b/src/editor/utils/print.ts @@ -1,5 +1,35 @@ import { PaperDirection } from '../dataset/enum/Editor' +function convertPxToPaperSize(width: number, height: number) { + if (width === 1125 && height === 1593) { + return { + size: 'a3', + width: '297mm', + height: '420mm' + } + } + if (width === 794 && height === 1123) { + return { + size: 'a4', + width: '210mm', + height: '297mm' + } + } + if (width === 565 && height === 796) { + return { + size: 'a5', + width: '148mm', + height: '210mm' + } + } + // 其他默认不转换 + return { + size: '', + width: `${width}px`, + height: `${height}px` + } +} + export interface IPrintImageBase64Option { width: number height: number @@ -24,10 +54,17 @@ export function printImageBase64( const doc = contentWindow.document doc.open() const container = document.createElement('div') + const paperSize = convertPxToPaperSize(width, height) base64List.forEach(base64 => { const image = document.createElement('img') - image.style.width = `${width}px` - image.style.height = `${height}px` + image.style.width = + direction === PaperDirection.HORIZONTAL + ? paperSize.height + : paperSize.width + image.style.height = + direction === PaperDirection.HORIZONTAL + ? paperSize.width + : paperSize.height image.src = base64 container.append(image) }) @@ -39,7 +76,9 @@ export function printImageBase64( } @page { margin: 0; - size: ${direction === PaperDirection.HORIZONTAL ? `landscape` : `portrait`}; + size: ${paperSize.size} ${ + direction === PaperDirection.HORIZONTAL ? `landscape` : `portrait` + }; }` style.append(document.createTextNode(stylesheet)) setTimeout(() => {