Merge pull request #202 from presenton/fix/pptx-export-issues
fix: division by zero, no paragraphs in text box model and elements with 0 width or height issue solved
This commit is contained in:
commit
9bd97d22ec
4 changed files with 19 additions and 3 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue