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

Docx file corrupted when image in header #354

Open
griprkevin opened this issue Mar 13, 2024 · 0 comments
Open

Docx file corrupted when image in header #354

griprkevin opened this issue Mar 13, 2024 · 0 comments

Comments

@griprkevin
Copy link

Hi!
I have an issue when creating reports with image in the docx header.

Example code

const { createReport } = require("docx-templates");
const sharp = require("sharp");

(async () => {
  const template = fs.readFileSync("./docx_template.docx");

  const data = {
    companyName: "My company",
    companyNr: "123",
    companyAddress: "Company street 1",
    companyZip: "12345",
    companyPlace: "Company city",
    companyPhone: "123456789",
    fileVersion: "1.0",
    fileUpdatedAt: "2021-01-01",
  };

  const reportOptions = {
    template,
    data,
    cmdDelimiter: ["{", "}"],
    noSandbox: true,
  };

  reportOptions.additionalJsContext = {
    companyLogo: async () => {
      const source = await sharp({
        create: {
          width: 200,
          height: 100,
          channels: 4,
          background: { r: 0, g: 0, b: 0, alpha: 0 },
        },
      })
        .png()
        .toBuffer();
      const data = source.toString("base64");

      return { width: 6, height: 6, data, extension: ".png" };
    },
  };

  const report = await createReport(reportOptions);

  fs.writeFileSync("report.docx", report);
})();

Example template: docx_template.docx

Result

The file wont open, it looks corrupted
image

Notes

I tested on node version "20.9.0" using docx-templates version "4.11.4"
The docx was made with the MS word online editor

If you need additional information, please let me know

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