Skip to content

Attempting to write a heading (in browser) gives error #485

Description

@daldred

I would like to use docx.js to write content from a PHP-based website into a Word document (to help a group administrator create a print newsletter using info already entered onto their website).

I'm trying to get started by writing a heading and a bit of text to the document using this code (adapted from the example of using docx.js in the browser, and the example of how to write heading in the docs:

<!DOCTYPE html>
<html>
<head>
  <script src="https://unpkg.com/docx@4.0.0/build/index.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.js"></script>
</head>

<body>

    <h1>DOCX browser Word document generation</h1>
    <button type="button" onclick="generate()">Click to generate new document</button>

    <script>
        function generate() {
            const doc = new Document();
            const paragraph1 = new Paragraph({
                  text: "This is the heading",
                  heading: HeadingLevel.HEADING_1
            });*/
            const paragraph2 = new Paragraph({
                  text: "This is the text"
            });
            doc.addParagraph(paragraph1);
            doc.addParagraph(paragraph2);
            const packer = new Packer();
            packer.toBlob(doc).then(blob => {
              console.log(blob);
              saveAs(blob, "example.docx");
              console.log("Document created successfully");
            });
        }
    </script>
</body>
</html>

This produces an error "Uncaught ReferenceError: HeadingLevel is not defined"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions