Demo
Readme
Changelog
Releases RSS Feed
Compatibility : nodejs and browser

Summary (version 3.0.3) Buy pptx-sub module

This module adds a tag to include the content of an other pptx document (which can be a template itself). This allows you to define subtemplates if your templates share common parts.

README

Subtemplate Module for pptx

This module exposes a tag to include subtemplates from pptx. The subtemplate is embeded into your presentation.

The styles of the resulting template should be the same as the styles from the included presentation, which means that styles from the included slides will be imported to the main template file.

Installation:

You will need docxtemplater v3: npm install docxtemplater

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

Usage

Your docx should contain the text: {:include subtemplate}.

In your data, the key subtemplate should be another docxtemplater instance (of a pptx document).

You can then use the following code

const PptxSubModule = require("docxtemplater-pptx-sub-module");
const pptxSubModule = new PptxSubModule({});

const includedZip = new PizZip(fs.readFileSync("included_presentation.docx"));
const includedPres = new Docxtemplater().loadZip(includedZip);

const zip = new PizZip(fs.readFileSync("template.pptx"));
const doc = new Docxtemplater().loadZip(zip);
doc.attachModule(pptxSubModule);
doc.setData({
    subtemplate: includedPres
});

doc.render();

In the 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/pptx-sub-module.js"></script>
    <script>
        function loadDocument(file) {
            return new Promise(function(resolve, reject) {
                PizZipUtils.getBinaryContent(file, function(error, content) {
                    if (error) {
                        return reject(errror);
                    }
                    resolve(content);
                });
            });
        }

        Promise.all([
            loadDocument("demo_template.pptx"),
            loadDocument("examples/demo_included.pptx")
        ])
            .then(function(documents) {
                var doc = new docxtemplater().loadZip(new PizZip(documents[0]));
                doc.attachModule(new DocxtemplaterPptxSubModule());
                var includedPres = new docxtemplater().loadZip(
                    new PizZip(documents[1])
                );
                doc.setData({
                    subtemplate: includedPres
                });
                doc.render();
                var out = doc.getZip().generate({
                    type: "blob",
                    mimeType:
                        "application/vnd.openxmlformats-officedocument.presentationml.presentation"
                });
                saveAs(out, "generated.pptx");
            })
            .catch(function(err) {
                console.log("error while loading documents");
                console.log(err);
            });
    </script>
</html>

After installing the module, you can use a working demo by running node sample.js.

Subtemplate Rendering

You can render the values in your subtemplate by using a custom parser and render the document with the current scope.

For example :

doc.setOptions({
    parser: function recursiveParser(tagString) {
        return {
            get(scope, data) {
                const isIncludeTag =
                    data &&
                    data.meta &&
                    data.meta.part &&
                    data.meta.part.module === "pro-xml-templating/include-pptx";
                let value = null;
                if (tagString === ".") {
                    value = scope;
                } else {
                    value = expressions.compile(tagString)(scope);
                }
                if (isIncludeTag && value) {
                    value.setData(scope);
                    value.render();
                }
                return value;
            }
        };
    }
});

CHANGELOG

3.0.3

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

3.0.2

Bugfix to work together with slides module, previous versions could produce corrupt documents

3.0.1

Do not fail if including pptx-sub with slides module (needs slide module 3.2.3 or higher)

3.0.0

Initial release

Edgar Hipp

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