docx icon indicating copy to clipboard operation
docx copied to clipboard

Footnote number duplicates based on number of exports (React).

Open seans84 opened this issue 3 years ago • 1 comments

I've created a repo showcasing this bug here: https://github.com/seans84/docx-export-duplicated-footnote-bug. The only relevant code is in the src/App.js file.

Each time the button "Generate doc" is clicked it exports the document but the footnote number is duplicated.

NOTE: The number is not duplicated where the footnote is used inside the text of the page, only in the footnote itself.

Text on Page (corrrect everytime): Screen Shot 2021-07-01 at 12 13 58 PM

First Export (correct): Screen Shot 2021-07-01 at 9 43 38 AM

Second Export (2x duplicated footnote number): Screen Shot 2021-07-01 at 9 43 47 AM

Third Export (3x duplicated footnote number): Screen Shot 2021-07-01 at 9 43 55 AM

App.js file

import "./App.css";

import { saveAs } from "file-saver";
import {
  Document,
  Packer,
  Paragraph,
  TextRun,
  FootnoteReferenceRun,
} from "docx";

const footnotes = {
  1: {
    children: [
      new Paragraph({
        text: "    This is the first footnote",
      }),
    ],
  },
  2: {
    children: [
      new Paragraph({
        text: "    This is the second footnote",
      }),
    ],
  },
};

function App() {
  const generate = () => {
    const doc = new Document({
      sections: [
        {
          children: [
            new Paragraph({
              style: "paragraph",
              children: [
                new TextRun({
                  children: ["Some Text.", new FootnoteReferenceRun(1)],
                }),
                new TextRun({
                  children: [
                    "Some Additional Text.",
                    new FootnoteReferenceRun(2),
                  ],
                }),
              ],
            }),
          ],
        },
      ],
      footnotes,
    });

    Packer.toBlob(doc).then((blob) => {
      saveAs(blob, "example.docx");
    });
  };

  return (
    <div>
      <p>
        Click the button and observe the footnote number duplicating every
        click.
        <button onClick={generate}>Generate doc</button>
      </p>
    </div>
  );
}

export default App;

seans84 avatar Jul 01 '21 15:07 seans84

@dolanmiu Have you had a chance to look at this, or any idea of what could be causing this issue?

seans84 avatar Jul 12 '21 16:07 seans84

This is fixed by inlining the footnotes

function App() {
    const generate = () => {
        const doc = new Document({
            sections: [
                {
                    children: [
                        new Paragraph({
                            style: "paragraph",
                            children: [
                                new TextRun({
                                    children: ["Some Text.", new FootnoteReferenceRun(1)],
                                }),
                                new TextRun({
                                    children: ["Some Additional Text.", new FootnoteReferenceRun(2)],
                                }),
                            ],
                        }),
                    ],
                },
            ],
            footnotes: {
                1: {
                    children: [
                        new Paragraph({
                            text: "    This is the first footnote",
                        }),
                    ],
                },
                2: {
                    children: [
                        new Paragraph({
                            text: "    This is the second footnote",
                        }),
                    ],
                },
            },
        });

        Packer.toBlob(doc).then((blob) => {
            saveAs(blob, "example.docx");
        });
    };

    return (
        <div>
            <p>
                Click the button and observe the footnote number duplicating every click.
                <button onClick={generate}>Generate doc</button>
            </p>
        </div>
    );
}

export default App;

dolanmiu avatar Oct 26 '22 23:10 dolanmiu