Skip to content

svg image doesn't render only when type: 'svg' is added, fallsback to png #3227

@abdalaosama

Description

@abdalaosama

I just spent about 3 hours pulling my hair out trying to figure out why SVG wasn't rendering properly.
And I am not sure if this is a problem with my environment or this specific SVG file.

I am writing this for anyone who may come across a similar issue in the future

function getSVGImageFromBuffer(imageBuffer, width = 100, height = 100) {

    return new ImageRun({
        type: 'svg',
        data: Buffer.from( imageBuffer.svg ),
        transformation: { width: width, height: height },
        fallback: {
            type: "png",
            data: imageBuffer.png,
            transformation: {
                width: width,
                height: height
            }
        },
    });
}

When trying to use it the way above, I get the following result
Image

But when commenting out the type: 'svg' line as follows:

function getSVGImageFromBuffer(imageBuffer, width = 100, height = 100) {

    return new ImageRun({
        // type: 'svg',
        data: Buffer.from( imageBuffer.svg ),
        transformation: { width: width, height: height },
        fallback: {
            type: "png",
            data: imageBuffer.png,
            transformation: {
                width: width,
                height: height
            }
        },
    });
}

I get a nice crisp SVG in the output docx file. works!

Image

Here is the original SVG file, in case you need it for debugging

Image

"docx": "^9.1.1", npm package version
node v20.16.0

I assume this is not the intended behaviour. I would love to help fix this
If there is any more info needed, I would love to provide it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions