Search code examples
docxhtml-to-docx

Docx.js not recognizing basic functions in html javascript


I'm trying to export a web-app to MS Word using Docx.js in the browser but basic functions like Table or Media are not being recognized by the browser. Has anyone experienced this issue and if so, what is there a fix?.

I'm receiving the following error message in console: "Uncaught ReferenceError: Table is not defined"

Below is my sample code:

<html>
<h1>DOCX browser Word document generation</h1>

<button type="button" onclick="generate()">Click to generate document</button>

<script>
    function generate() {
        const doc = new docx.Document();
        
        const table = new Table({
            rows: [
                new TableRow({
                    children: [
                        new TableCell({
                            children: [new Paragraph("Hello")],
                        }),
                        new TableCell({
                            children: [new Paragraph("World!!!")],
                        }),
                    ],
                }),
                new TableRow({
                    children: [
                        new TableCell({
                            children: [new Paragraph("bla bla bla")],
                        }),
                        new TableCell({
                            children: [new Paragraph("bla bla bla")],
                        }),
                    ],
                }),
            ],
        });

        doc.addSection({
            properties: {},
            children: [table],
        });

        docx.Packer.toBlob(doc).then(blob => {
            console.log(blob);
            saveAs(blob, "example.docx");
            console.log("Document created successfully");
        });
    }
</script>

Solution

  • I was having a similar problem in node. I was getting ReferenceError: HeadingLevel is not defined. Researching this I found this issue opened in github https://github.com/dolanmiu/docx/issues/485. So I think you need to explicitly call docx before any of the docx declarations. For example.

                rows: [
                    new docx.TableRow({
                        children: [
                            new docx.TableCell({
                                children: [new docx.Paragraph("Hello")],
                            }),
                            new docx.TableCell({
                                children: [new docx.Paragraph("World!!!")],
                            }),
                        ],
                    }),
                    new docx.TableRow({
                        children: [
                            new docx.TableCell({
                                children: [new docx.Paragraph("bla bla bla")],
                            }),
                            new docx.TableCell({
                                children: [new docx.Paragraph("bla bla bla")],
                            }),
                        ],
                    }),
                ],
            });