You are currently offline, serving cached version

Usage (nodejs)

var PPModule = require("docxtemplater-pp-module");

var ppModule = new PPModule({});

var zip = new PizZip(content);
var doc = new Docxtemplater(zip, {
    modules: [ppModule],
});

doc.render({
    company_name: "Acme Corp",
    company_shareprice: 20.93,
});

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

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

In the browser, use following html file :

<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/pp-module.js"></script>
    <script>
        PizZipUtils.getBinaryContent(
            "examples/demo.docx",
            function (error, content) {
                if (error) {
                    console.error(error);
                    return;
                }

                var ppModule = new DocxtemplaterPPModule({});
                var zip = new PizZip(content);
                var doc = new docxtemplater(zip, {
                    modules: [ppModule],
                });

                doc.render({
                    company_name: "Acme Corp",
                    company_shareprice: 20.93,
                });
                var out = doc.getZip().generate({
                    type: "blob",
                    mimeType:
                        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
                });
                saveAs(out, "generated.docx");
            }
        );
    </script>
</html>

Behavior

Your docx should contain the text: {?data}. After installing the module, you can use a working demo by running node sample.js.

When a tag beginning with a question mark (?) is found, the tag value is calculated.

  • If the tag value is falsy the whole paragraph containing the tag is removed.

  • If the tag is true, the paragraph is kept but the tag is replaced to "".

  • If the tag value is truthy, the tag is replaced normally and the paragraph is kept.

If there are multiple "paragraph placeholder" tags in the same paragraph, the paragraph will be removed if one of the values is falsy. Only if all values are truthy, the paragraph is kept.

CHANGELOG

3.3.0

Make module compatible with docxtemplater@3.28.0. Please make sure to update docxtemplater to 3.28.0 at the same time you update this module. The internal change made is the use of the new matchers API which fixes bugs that were triggered depending on the order of the modules that are attached to the instance. Now the order of the modules should not matter as expected.

3.2.5

Use @xmldom/xmldom instead of xmldom, see this github issue

3.2.4

Generate files in built with correct filename In previous versions, the filename was always build/docxtemplater.js. Now the filename is error-location-module.js The .min.js file is also created now.

3.2.3

Add typescript definitions for public API

3.2.2

Bugfix "Cannot read property 'offset' of undefined, when using multiple paragraph placeholder tags in the same paragraph

3.2.1

Declare supportedFileTypes, which allows to use this module with the new docxtemplater constructor which was introduced in docxtemplater 3.17.

3.2.0

Hide value if value === true (in that case, show the paragraph but don't display any value)

3.1.0

Add support for pptx format (Powerpoint files)

3.0.0

Initial release