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.
Your docx should contain the text: {:include subtemplate}
.
In your data, the key subtemplate should be another docxtemplater instance (of a pptx document).
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,
});
<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
.
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);
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],
});
Bugfix to fix following error :
Bugfix when using section loops with slides module when used together with the pptx-subtemplate module
Make it possible to use prefix from the constructor
Add typescript typings to be able to change the module prefix
Fix regression : correctly template slideMaster files (if some {placeholders} are present in the slideMaster, those will now be correctly templated).
Upgrade module to use NodeNext moduleResolution setting. See explanation here
Bugfix to make module work when used together with docxtemplater 3.42.0 and above.
Set module.priority in order to have no more issues related to module ordering
Add module.clone() internal to make module work well with subsegment module
Make package size smaller
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.
Use @xmldom/xmldom instead of xmldom, see this github issue
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.
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.
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.
Bugfix two kind of corruptions :
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.
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.
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
By default, copy all relationships instead of having to explicitly copy a subset of the relationships (this behavior should avoid some corruptions).
Add support for including pptx files with diagrams
Add typescript definitions for public API
Move webpack from dependency to devDependency (which triggered an error during installation on node version 10)
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.
Declare supportedFileTypes, which allows to use this module with the new docxtemplater constructor which was introduced in docxtemplater 3.17.
Bugfix to work together with slides module, previous versions could produce corrupt documents
Do not fail if including pptx-sub with slides module (needs slide module 3.2.3 or higher)
Initial release