From 56cd69a8b6ab19f7e75f676a82899915b09d916a Mon Sep 17 00:00:00 2001 From: sauravniraula Date: Sun, 10 Aug 2025 17:52:52 +0545 Subject: [PATCH] fix: division by zero, no paragraphs in text box model and elements with 0 width or height issue solved --- servers/fastapi/models/pptx_models.py | 8 ++++++-- .../nextjs/app/api/presentation_to_pptx_model/route.ts | 6 +++++- servers/nextjs/types/pptx_models.ts | 4 ++++ servers/nextjs/utils/pptx_models_utils.ts | 4 ++++ 4 files changed, 19 insertions(+), 3 deletions(-) diff --git a/servers/fastapi/models/pptx_models.py b/servers/fastapi/models/pptx_models.py index ad596481..ee1e4cd1 100644 --- a/servers/fastapi/models/pptx_models.py +++ b/servers/fastapi/models/pptx_models.py @@ -1,5 +1,5 @@ from enum import Enum -from typing import Annotated, List, Optional +from typing import Annotated, List, Literal, Optional from annotated_types import Len from pydantic import BaseModel from pptx.util import Pt @@ -105,10 +105,11 @@ class PptxPictureModel(BaseModel): class PptxShapeModel(BaseModel): - pass + shape_type: Literal["textbox", "autoshape", "picture", "connector"] class PptxTextBoxModel(PptxShapeModel): + shape_type: Literal["textbox"] = "textbox" margin: Optional[PptxSpacingModel] = None fill: Optional[PptxFillModel] = None position: PptxPositionModel @@ -117,6 +118,7 @@ class PptxTextBoxModel(PptxShapeModel): class PptxAutoShapeBoxModel(PptxShapeModel): + shape_type: Literal["autoshape"] = "autoshape" type: MSO_AUTO_SHAPE_TYPE = MSO_AUTO_SHAPE_TYPE.RECTANGLE margin: Optional[PptxSpacingModel] = None fill: Optional[PptxFillModel] = None @@ -129,6 +131,7 @@ class PptxAutoShapeBoxModel(PptxShapeModel): class PptxPictureBoxModel(PptxShapeModel): + shape_type: Literal["picture"] = "picture" position: PptxPositionModel margin: Optional[PptxSpacingModel] = None clip: bool = True @@ -141,6 +144,7 @@ class PptxPictureBoxModel(PptxShapeModel): class PptxConnectorModel(PptxShapeModel): + shape_type: Literal["connector"] = "connector" type: MSO_CONNECTOR_TYPE = MSO_CONNECTOR_TYPE.STRAIGHT position: PptxPositionModel thickness: float = 0.5 diff --git a/servers/nextjs/app/api/presentation_to_pptx_model/route.ts b/servers/nextjs/app/api/presentation_to_pptx_model/route.ts index 3c25bf42..f6dcd04d 100644 --- a/servers/nextjs/app/api/presentation_to_pptx_model/route.ts +++ b/servers/nextjs/app/api/presentation_to_pptx_model/route.ts @@ -179,7 +179,7 @@ const convertSvgToPng = async (element_attibutes: ElementAttributes) => { const svgBuffer = Buffer.from(svgHtml); const pngBuffer = await sharp(svgBuffer) - .resize(Math.round(element_attibutes.position?.width ?? 10), Math.round(element_attibutes.position?.height ?? 10)) + .resize(Math.round(element_attibutes.position!.width!), Math.round(element_attibutes.position!.height!)) .toFormat('png') .toBuffer(); return pngBuffer; @@ -261,6 +261,10 @@ async function getAllChildElementsAttributes({ element, rootRect = null, depth = }; } + if (attributes.position === undefined || attributes.position.width === undefined || attributes.position.height === undefined || attributes.position.width === 0 || attributes.position.height === 0) { + continue; + } + if (attributes.tagName === 'svg' || attributes.tagName === 'canvas' || attributes.tagName === 'table') { attributes.should_screenshot = true; attributes.element = childElementHandle; diff --git a/servers/nextjs/types/pptx_models.ts b/servers/nextjs/types/pptx_models.ts index 7ece780d..dc35cdf2 100644 --- a/servers/nextjs/types/pptx_models.ts +++ b/servers/nextjs/types/pptx_models.ts @@ -281,6 +281,7 @@ export interface PptxShapeModel { } export interface PptxTextBoxModel extends PptxShapeModel { + shape_type: string; margin?: PptxSpacingModel; fill?: PptxFillModel; position: PptxPositionModel; @@ -289,6 +290,7 @@ export interface PptxTextBoxModel extends PptxShapeModel { } export interface PptxAutoShapeBoxModel extends PptxShapeModel { + shape_type: string; type?: PptxShapeType; margin?: PptxSpacingModel; fill?: PptxFillModel; @@ -301,6 +303,7 @@ export interface PptxAutoShapeBoxModel extends PptxShapeModel { } export interface PptxPictureBoxModel extends PptxShapeModel { + shape_type: string; position: PptxPositionModel; margin?: PptxSpacingModel; clip: boolean; @@ -313,6 +316,7 @@ export interface PptxPictureBoxModel extends PptxShapeModel { } export interface PptxConnectorModel extends PptxShapeModel { + shape_type: string; type?: PptxConnectorType; position: PptxPositionModel; thickness: number; diff --git a/servers/nextjs/utils/pptx_models_utils.ts b/servers/nextjs/utils/pptx_models_utils.ts index ff72b341..e314f647 100644 --- a/servers/nextjs/utils/pptx_models_utils.ts +++ b/servers/nextjs/utils/pptx_models_utils.ts @@ -132,6 +132,7 @@ function convertToTextBox(element: ElementAttributes): PptxTextBoxModel { }; return { + shape_type: "textbox", margin: undefined, fill, position, @@ -190,6 +191,7 @@ function convertToAutoShapeBox(element: ElementAttributes): PptxAutoShapeBoxMode } return { + shape_type: "autoshape", type: shapeType, margin: undefined, fill, @@ -220,6 +222,7 @@ function convertToPictureBox(element: ElementAttributes): PptxPictureBoxModel { }; return { + shape_type: "picture", position, margin: undefined, clip: element.clip ?? true, @@ -241,6 +244,7 @@ function convertToConnector(element: ElementAttributes): PptxConnectorModel { }; return { + shape_type: "connector", type: PptxConnectorType.STRAIGHT, position, thickness: element.border?.width ?? 0.5,