dolanmiu / docx

Easily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
https://docx.js.org/
MIT License
4.36k stars 484 forks source link

Wrong DOCX format when using Google Docs #1794

Open yukal opened 1 year ago

yukal commented 1 year ago

My generated Docx file seems corrupted when I try to view it on Google Docs or when I share it by email with someone else. I upload the generated Docx file to Google Docs, and when I try to read it, I see a black page. Someone tries to read it by email and can't open it, they see a black page too.

dolanmiu commented 1 year ago

Can you send me some reproduction steps? The code you used to build the docx file?

rizedr commented 1 year ago

Getting something similar with a fresh generated document. image

However, if I take that generated docx:

  1. Open in Microsoft Word
  2. Do a small edit
  3. Save
  4. Upload to google drive => The document opens with no issue in google docs this time.

Trying to reproduce the issue at a smaller case (dealing with a big docx right now & will follow up).

dolanmiu commented 1 year ago

Thank you yes please, create some reproduction steps

rizedr commented 1 year ago

Yes, so TLDR; the issue (in my case at least) is because of defining the Normal paragraphStyles basedOn Normal -- this is what caused Google Docs to error.

const doc = new Document({
      title: 'Google test',
      styles: {
        paragraphStyles: [
          {
            id: 'Normal',
            name: 'Normal',
            basedOn: 'Normal', // the error
            next: 'Normal',
            quickFormat: true,
            run: {
              font: 'Helvetica Neue',
              size: 24,
            },
            paragraph: {
              alignment: AlignmentType.LEFT,
              spacing: {
                line: 240 * 1.15,
              },
            },
          }
        ],
      },
      sections: [
        {
          children: [
            new Paragraph({
              text: 'Hello world',
            }),
          ],
        },
      ],
    });

So it was just that -- not sure it is the same issue as the OP had, I've certainly did not see a black page & the gmail previews would work just fine.

I think you can close this as IMO is not a bug. Maybe just a docs update to include details on how to change the Normal text paragraph in the library.

dolanmiu commented 1 year ago

Is this because it is illegal to base a style on itself? Does removing that basedOn line fix it?

rizedr commented 1 year ago

Apparently for Google Docs, yes is illegal, causes their app to crash.

And yes, if you remove that line it fixes the issue.

Furthermore, if you base a style on itself, generate the doc, and then, if you open in Microsoft Word & save the file, it will remove that basedOn from the file (works afterwards when you upload to the Google Docs black box)