You are currently offline, serving cached version

Summary

Important : This module only supports xlsx (Excel documents), not docx, see the html if you want to include HTML inside Word documents.

The module also needs the XLSX module to work.

To achieve that, simply add "~~" in front of your field name. The final syntax should be: {~~myField}

You can then write in your data, the following :

const HTMLXlsxModule = require("docxtemplater-html-xlsx-module");
const doc = new Docxtemplater(zip, {
    modules: [new HTMLXlsxModule({})],
});
doc.render({ myField: "<b>Hello</b>, Foo !" });

Usage

Usage (nodejs)

const HTMLXlsxModule = require("docxtemplater-html-xlsx-module");
const XlsxModule = require("docxtemplater-xlsx-module");
const fs = require("fs");

const doc = new Docxtemplater(zip, {
    modules: [new XlsxModule(), new HTMLXlsxModule({})],
}).render({ html: "<b>Hello</b>, Foo !" });

const buffer = doc.getZip().generate({
    type: "nodebuffer",
    compression: "DEFLATE",
});

fs.writeFile("test.xlsx", buffer);

Usage (browser)

<html>
    <script src="node_modules/docxtemplater/build/docxtemplater.js"></script>
    <script src="node_modules/pizzip/dist/pizzip.js"></script>
    <script src="node_modules/pizzip/vendor/FileSaver.js"></script>
    <script src="node_modules/pizzip/dist/pizzip-utils.js"></script>
    <script src="build/xlsxmodule.js"></script>
    <script src="build/html-xlsx-module.js"></script>
    <script>
        PizZipUtils.getBinaryContent(
            "examples/input.xlsx",
            function (error, content) {
                if (error) {
                    console.error(error);
                    return;
                }

                const zip = new PizZip(content);
                const doc = new docxtemplater(zip, {
                    modules: [
                        new DocxtemplaterXlsxModule(),
                        new DocxtemplaterXlsxHtmlModule({}),
                    ],
                });

                doc.render({
                    html: "<p>Hello <b>John</b></p>",
                });
                const out = doc.getZip().generate({
                    type: "blob",
                    mimeType:
                        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                });
                saveAs(out, "generated.xlsx");
            }
        );
    </script>
</html>

Supported tags

This module currently supports:

  • <a href="URL">Linktext</a>
  • <b>, <u> and <i> tags
  • <strong>, <em> and <span> tags
  • <ul>, <ol>, and <li>
  • <br>
  • font-size: 15px
  • color: #770

Styling the full cell

It is possible to style the full cell (changing the background color, text-align or borders).

Use it like this :

{~~html}
const HTMLXlsxModule = require("docxtemplater-html-xlsx-module");
const doc = new Docxtemplater(zip, {
    modules: [new HTMLXlsxModule({})],
});
doc.render({
    myField: {
        html: "<b>Hello</b>, Foo !",
        cellStyle: `
                background-color: #008ff0;
                text-align: right;
                border-bottom: 3px dashed #f0f;
            `,
    },
});

CHANGELOG

3.2.3

When using the HTML tag and inserting links, there was a performance issue when adding over 1000 links in a document.

The more links you added, the slower it got.

This is now fixed, and the time spent should be proportional to the number of links added (10000 links should still take about 2 seconds). Previously, adding 10000 links could easily take 30 seconds.

3.2.2

  • When including the module using webpack, throw specific error if cssParser could not be loaded.

See this Create React App issue and this @adobe/css-tools issue.

3.2.1

Add support for <figure> and <figcaption> tag (which renders exactly like a div).

3.2.0

Make it possible to style the full cell : border, text-align, background-color;

3.1.11

Bugfix when using special characters in links.

Previously, the following code would produce a corrupt document :

const HTMLXlsxModule = require("docxtemplater-html-xlsx-module");
const XlsxModule = require("docxtemplater-xlsx-module");
const doc = new Docxtemplater(zip, {
    modules: [new XlsxModule(), new HTMLXlsxModule({})],
});
doc.render({
    html: "<a href=\"https://ddg.gg/&gt;''\">&lt; && \"DDG' & &gt; XXX</a>",
});

Now the generated document is valid.

3.1.10

Upgrade module to use NodeNext moduleResolution setting. See explanation here

3.1.9

Bugfix issue when having tag inside "sheetname" (a normal tag, not an HTML tag). The value shown would simply be "true".

3.1.8

Use new resolvedId API => requires docxtemplater@3.43.0

3.1.7

Add support for setting ignoreUnknownTags:true

3.1.6

Fix bug when using renderAsync and parser compilation fails : the error should now be shown inside a multierror. To to this, we know use sm.getValueAsync in resolve instead of sm.getValue

3.1.5

Set module.priority in order to have no more issues related to module ordering

3.1.4

Bugfix issue when using expand horizontal loop in xlsx :

TypeError: Cannot read properties of undefined (reading 'fontId')

3.1.3

Bugfix issue when using loop module with HtmlXlsxModule enabled, the following template :

{#items}
{#x}1{/}{#y}2{/y}
{/}

Would produce :

In async mode :

Cannot read properties of undefined (reading 'value')
at ScopeManager.\_getValue (node_modules/docxtemplater/js/scope-manager.js:43:7)"

In sync mode :

Cannot read properties of undefined (reading 'forEach')
at loopOver (js/index.js)

3.1.2

Add module.clone() internal

3.1.1

Bugfix when passing in empty string : "" or blank string : " ".

Previously, the following error was thrown :

Cannot read properties of undefined (reading 'childNodes')

3.1.0

Add support for :

  • <ul>, <ol>, and <li>
  • <br>
  • font-size: 15px
  • color: #770

Bugfix for <strong>, <em>, <i>, <u>.

3.0.0

Initial release

Talk with sales Contact us