You are currently offline, serving cached version
10 December 2024 : If you are using angular-expressions to parse expressions such as {user.name}, {#users.length > 10}, please upgrade asap to angular-expressions@1.4.3 for security reasons : View Github issue

Pptx-subtemplate module

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.

Usage

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

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

Usage (nodejs)

const fs = require("fs");
const PptxSubModule = require("docxtemplater-pptx-sub-module");

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(zip, {
    modules: [new PptxSubModule({})],
});
doc.render({
    subtemplate: includedPres,
});

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/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(error);
                        }
                        resolve(content);
                    }
                );
            });
        }

        Promise.all([
            loadDocument("demo_template.pptx"),
            loadDocument("examples/demo_included.pptx"),
        ])
            .then(function (documents) {
                const doc = new docxtemplater(
                    new PizZip(documents[0]),
                    {
                        modules: [
                            new DocxtemplaterPptxSubModule(),
                        ],
                    }
                );
                const includedPres = new docxtemplater().loadZip(
                    new PizZip(documents[1])
                );
                doc.render({
                    subtemplate: includedPres,
                });
                const 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 :

const expressionParser = require("docxtemplater/expressions.js");
const doc = new Docxtemplater(zip, {
    parser: function recursiveParser(tagString) {
        const parser = expressionParser(tagString);
        return {
            get(scope, context) {
                const isIncludeTag =
                    context &&
                    context.meta &&
                    context.meta.part &&
                    context.meta.part.module ===
                        "pro-xml-templating/include-pptx";
                const value = parser.get(scope, context);
                if (isIncludeTag && value) {
                    value.render(scope);
                }
                return value;
            },
        };
    },
});
doc.render(data);

Including multiple documents with a single tag

Note that it is possible to include several documents using the tag : {:include docs}, by simply using an array of documents in the related data attribute.

const fs = require("fs");
const PptxSubModule = require("docxtemplater-pptx-sub-module");

const includedPres1 = new Docxtemplater(
    new PizZip(fs.readFileSync("included1.docx"))
);
const includedPres2 = new Docxtemplater(
    new PizZip(fs.readFileSync("included2.docx"))
);
const doc = new Docxtemplater(zip, {
    modules: [new PptxSubModule({})],
});
doc.render({
    docs: [includedPres1, includedPres2],
});

CHANGELOG

3.1.10

Bugfix to fix following error :

TypeError: qualifiedName.toLowerCase is not a function
at Document.getElementsByTagName (/node_modules/@xmldom/xmldom/lib/dom.js:2527:42)
at forEachTag (node_modules/docxtemplater-pptx-sub-module/js/slide-include.js:90:21)
at rescale (node_modules/docxtemplater-pptx-sub-module/js/slide-include.js:275:7)

3.1.9

Bugfix when using section loops with slides module when used together with the pptx-subtemplate module

3.1.8

Make it possible to use prefix from the constructor

3.1.7

Add typescript typings to be able to change the module prefix

3.1.6

Fix regression : correctly template slideMaster files (if some {placeholders} are present in the slideMaster, those will now be correctly templated).

3.1.5

Upgrade module to use NodeNext moduleResolution setting. See explanation here

3.1.4

Bugfix to make module work when used together with docxtemplater 3.42.0 and above.

3.1.3

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

3.1.2

Add module.clone() internal to make module work well with subsegment module

3.1.1

Make package size smaller

3.1.0

Bugfix to correctly remove dropped files also in ContentTypes

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.0.17

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

3.0.16

Add support to include all slideLayouts if using the option importAllSlideLayouts in the constructor, like this :

const PptxSubModule = require("docxtemplater-pptx-sub-module");
const fs = require("fs");
const zip = new PizZip(fs.readFileSync("template.pptx"));
const doc = new Docxtemplater(zip, {
    modules: [
        new PptxSubModule({
            importAllSlideLayouts: true,
        }),
    ],
});
doc.render(data);

In this case, when setting the option to true, all slideMasters that are used by at least one slide will automatically import all slideLayouts attached to the given slideMaster.

3.0.15

Bugfix issue when using pptx-subtemplate module together with the slides module.

In previous versions of the pptx-subtemplate module, the generated document could be corrupted when attaching the slides module as well.

Also, in previous versions of the pptx-subtemplate module, when also attaching the slides module, only the first slide of the document would be included (repeatedly). Now, all slides are attached as expected.

3.0.14

Bugfix stacktrace : Cannot read property 'asText' of undefined.

This error message would happen on some documents that are included.

Bugfix issue where the slides were not keeping the right order (using an inversed order in some occasions).

Correctly scale the size of each table when the slide size differs.

3.0.13

Bugfix two kind of corruptions :

  • The first kind of corruption occured when including another presentation that would use two different layouts but the same slide master (pretty common case). (This corruption was introduced in version 3.0.12).
  • The second kind of corruption occured when having many slideMasters files in the output document (more than 25 slideMaster files), and having at least one unused slide master file. The module will now automatically remove unused slide master files when there are over 20 slide master files.

3.0.12

Bugfix to avoid creating multiple copies of the same slideMaster file when including multiple slides from the same presentation.

This also avoids copying the same internal files, and will instead reuse the existing image file, resulting in smaller output sizes for the document.

3.0.11

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

3.0.10

Bugfix issue when including multiple powerpoint files that use the same image name, but with different casing.

For example, if you included two powerpoint files, one which has an image called /media/image1.png, and in an other file, you have an image called /media/image1.PNG, the generated document would be come corrupt.

This version fixes this corruption

3.0.9

By default, copy all relationships instead of having to explicitly copy a subset of the relationships (this behavior should avoid some corruptions).

3.0.8

Add support for including pptx files with diagrams

3.0.7

Add typescript definitions for public API

3.0.6

Move webpack from dependency to devDependency (which triggered an error during installation on node version 10)

3.0.5

Internal change to allow to match tags with non-breaking space.

When entering Ctrl+Shift+Space, a "non-breaking" space is created.

When using a non-breaking space in the tag {:include doc}, the tag would not be replaced correctly.

3.0.4

  • Bugfix to not corrupt the resulting document when there are charts with External Data (excel sheet) present in the subtemplate.
  • Bugfix to correctly include images when they are present in the Slide Layout.

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

Talk with sales Contact us