Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Repeated images are written several times #2680

Open
mustache1up opened this issue May 5, 2024 · 2 comments
Open

Repeated images are written several times #2680

mustache1up opened this issue May 5, 2024 · 2 comments

Comments

@mustache1up
Copy link

mustache1up commented May 5, 2024

When using the same image in the document, each instance of the image results in a different file inside the generated docx file.

Documents with reused images gets linearly bigger.

Minimum example:

import { Document, ImageRun, Packer, Paragraph } from "docx";

const imageBase64Data = "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAFUlEQVR42mP8z8BQz0AEYBxVSF+FABJADveWkH6oAAAAAElFTkSuQmCC";

const doc = new Document({
    sections: [
        {
            children: [
                new Paragraph({
                    children: [
                        new ImageRun({
                            data: imageBase64Data, // image
                            transformation: {
                                width: 100,
                                height: 100,
                            },
                        }),
                    ],
                }),
                new Paragraph({
                    children: [
                        new ImageRun({
                            data: imageBase64Data, // same image
                            transformation: {
                                width: 200,
                                height: 200,
                            },
                        }),
                    ],
                }),
            ],
        },
    ],
});

Packer.toBuffer(doc).then((buffer) => {
    fs.writeFileSync("same_image_twice.docx", buffer); // two identical media files inside the docx
});
@mustache1up
Copy link
Author

We could use the git uses for a while now, using a digest of the file as uniqueId of the image.

Git uses SHA1, I'll test if it work well in the ImageRun class.

@mustache1up
Copy link
Author

Seems to work great. Adding tests to spec files in order to open a PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant