You are currently offline, serving cached version

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

Usage (nodejs)

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

doc.render();

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

        Promise.all([
            loadDocument("demo_template.pptx"),
            loadDocument("examples/demo_included.pptx"),
        ])
            .then(function (documents) {
                var doc = new docxtemplater(
                    new PizZip(documents[0]),
                    {
                        modules: [
                            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 :

const doc = new Docxtemplater(zip, {
    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";
                const value =
                    tagString === "."
                        ? scope
                        : expressions.compile(tagString)(scope);
                if (isIncludeTag && value) {
                    value.setData(scope);
                    value.render();
                }
                return value;
            },
        };
    },
});

CHANGELOG

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 zip = new PizZip(fs.readFileSync("template.pptx"));
const pptxSubModule = new PptxSubModule({
    importAllSlideLayouts: true,
});
const doc = new Docxtemplater(zip, {
    modules: [pptxSubModule],
});

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 error-location-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