gitbrent / PptxGenJS

Create PowerPoint presentations with a powerful, concise JavaScript API.
https://gitbrent.github.io/PptxGenJS/
MIT License
2.62k stars 611 forks source link

[BUG] Aspect ratio calculation wrong for Images with one dimension above 100 px and one below #1286

Open Anderl1989 opened 11 months ago

Anderl1989 commented 11 months ago

Issue Category

Product Versions

Desired Behavior

Placing an image with one dimension above 100px and one dimension below 100px should calculate aspect ratio correctly.
The output srcRect of the code example below should be

<a:srcRect l="0" r="0" t="-20047" b="-20047"/>

Observed Behavior

Aspect ratio is calculated wrong, because it interprets the dimension below 100px as inches and the one above 100px as EMU. The output srcRect of the code example below is

<a:srcRect l="-32635313919" r="-32635313919" t="0" b="0"/>

The generated pptx File can't even be opened by PowerPoint without a warning that the file is damaged.

It is caused by this Line of code:
https://github.com/gitbrent/PptxGenJS/blob/035cf6b26239e2ea1f27eefbcdb8134e504eb4f4/src/gen-utils.ts#L25

When adding a breakpoint here:
https://github.com/gitbrent/PptxGenJS/blob/035cf6b26239e2ea1f27eefbcdb8134e504eb4f4/src/gen-xml.ts#L65
the code example below shows the following value for imgSize:

imgSize: {w: 2899, h: 88696800}

I'd recommend to either allow adding other units when adding w/h values as String, OR don't handle w/h values separately, usually they should use the same unit, OR don't bother converting these values to EMU at all, because they are only used for an aspect-ratio calculation, so the result stays the same, no matter if its inches or emu or something else (but %).

Steps to Reproduce

import pptxgen from "pptxgenjs";

let pres = new pptxgen();
let slide = pres.addSlide();

slide.addImage({
    "x": "19%",
    "y": "54%",
    // the next two lines are the important part. this is the image size in pixels
    "w": 2899,
    "h": 97,
    "path": "<PATH_TO_IMAGE>",
    "sizing": {
        "type": "contain",
        "w": "36%",
        "h": "3%",
    }
});

pres.writeFile({ fileName: "Test.pptx" });

Workaround

As a workaround I'm currently checking if one of the dimensions is below 100 and if so, I multiply both dimensions by a value that lets it be over 100. This is working because those dimensions are only used for aspect ratio calculation and the result of that calculation is not changed by such a scale.