Demo
Readme
Changelog
Releases RSS Feed
Compatibility : nodejs and browser

Summary (version 3.0.0) Buy paragraph-placeholder module

This module makes it possible to add a placeholder to a paragraph and automatically drop the paragraph if the placeholder evaluates to null or undefined

README

PP Module

Installation:

You will need docxtemplater v3: npm install docxtemplater

Install this module with npm install --save "$url"

Usage

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

var opts = {};

var ppModule = new PPModule(opts);

var zip = new PizZip(content);
var docx = new Docxtemplater()
    .attachModule(ppModule)
    .loadZip(zip)
    .setData({
        company_name: "Acme Corp",
        company_shareprice: 20.93
    })
    .render();

var buffer = docx
    .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 opts = {};

            var ppModule = new DocxtemplaterPPModule(opts);
            var zip = new PizZip(content);
            var doc = new docxtemplater()
                .loadZip(zip)
                .attachModule(ppModule)
                .compile();

            doc.setData({
                company_name: "Acme Corp",
                company_shareprice: 20.93
            });
            doc.render();
            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 null or undefined, the whole paragraph containing the tag is removed

  • If the tag value is not null nor undefined, 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 evaluates to null or undefined. Only if all values evaluate to something different, the paragraph is kept.

CHANGELOG

3.0.0

Initial release

Edgar Hipp

I'm the creator of docxtemplater. I work on making docxtemplater great since 2013.