This module exposes a tag to include a subtemplate (another Word document). The subtemplate is embeded into your document. Only the document content is embedded, footers and headers are not since they are unique for each document, so the module keeps the headers/footers from your base template.
The styles of the resulting generated document will be the same as the styles from the main template, which means that styles that exist in both the template and the included document will use the styles of the main template. Hence, if both your main template and your included document have Heading1
, the style that will be applied to all Heading1
will be the style from the main template.
The subtemplate module and the subsection module serve distinct purposes. The subtemplate module facilitates the insertion of another document's content into the current one, specifically focusing on the main content (no headers/footers). In contrast, the subsection module specifically handles the inclusion of headers and footers from an external document while excluding its main content (This is what Word calls a "section"). It's important to note that by utilizing both the subtemplate and subsection modules, one can seamlessly integrate the entirety of an external document, encompassing headers, footers, and main content.
Your docx should contain the text: {:include subtemplate}
.
In your data, the key subtemplate should be another docxtemplater instance.
const SubtemplateModule = require("docxtemplater-subtemplate-module");
const fs = require("fs");
const headerZip = new PizZip(fs.readFileSync("header.docx"));
const footerZip = new PizZip(fs.readFileSync("footer.docx"));
const headerDoc = new Docxtemplater().loadZip(headerZip);
const footerDoc = new Docxtemplater().loadZip(footerZip);
const zip = new PizZip(fs.readFileSync("template.docx"));
const doc = new Docxtemplater(zip, {
modules: [new SubtemplateModule({})],
});
doc.render({
subtemplate: headerDoc,
footer: footerDoc,
});
<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/subtemplate-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);
}
);
});
}
function parser(tag) {
return {
get(scope, context) {
const isIncludeTag =
context &&
context.meta &&
context.meta.part &&
context.meta.part.module ===
"pro-xml-templating/include";
const value = scope[tag];
if (isIncludeTag && value) {
if (value instanceof Array) {
return Promise.all(
value.map(function (v) {
return loadDocument(v).then(
function (document) {
const doc =
new docxtemplater(
new PizZip(
document
),
{
paragraphLoop: true,
parser,
linebreaks: true,
}
);
doc.render(scope);
return doc;
}
);
})
);
}
return loadDocument(value).then(
function (document) {
const doc = new docxtemplater(
new PizZip(document),
{
paragraphLoop: true,
parser,
linebreaks: true,
}
);
doc.render(scope);
return doc;
}
);
}
return value;
},
};
}
loadDocument("demo_template.docx")
.then(function (document) {
const doc = new docxtemplater(
new PizZip(document),
{
parser,
paragraphLoop: true,
linebreaks: true,
modules: [
new DocxtemplaterSubtemplateModule(),
],
}
);
return doc
.renderAsync({
header: "examples/demo_header.docx",
footer: "examples/demo_footer.docx",
})
.then(function () {
const out = doc.getZip().generate({
type: "blob",
mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
});
saveAs(out, "generated.docx");
});
})
.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 SubtemplateModule = require("docxtemplater-subtemplate-module");
const expressionParser = require("docxtemplater/expressions.js");
const fs = require("fs");
function parser(tagString) {
const exp = expressionParser(tagString);
return {
get(scope, context) {
const isIncludeTag =
context &&
context.meta &&
context.meta.part &&
context.meta.part.module ===
"pro-xml-templating/include";
const value = exp.get(scope, context);
if (isIncludeTag && value) {
if (value instanceof Array) {
return value.map(function (v) {
const zip = new PizZip(
fs.readFileSync(v)
);
const doc = new Docxtemplater(zip, {
paragraphLoop: true,
parser,
linebreaks: true,
});
doc.render(scope);
return doc;
});
}
const zip = new PizZip(fs.readFileSync(value));
const doc = new Docxtemplater(zip, {
paragraphLoop: true,
parser,
linebreaks: true,
});
doc.render(scope);
return doc;
}
return value;
},
};
}
const doc = new Docxtemplater(zip, {
modules: [new SubtemplateModule({})],
parser,
});
doc.render({
header: "examples/demo_header.docx",
footer: "examples/demo_footer.docx",
});
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 SubtemplateModule = require("docxtemplater-subtemplate-module");
const expressionParser = require("docxtemplater/expressions.js");
const fs = require("fs");
function parser(tagString) {
const exp = expressionParser(tagString);
return {
get(scope, context) {
const isIncludeTag =
context &&
context.meta &&
context.meta.part &&
context.meta.part.module ===
"pro-xml-templating/include";
const value = exp.get(scope, context);
if (isIncludeTag && value) {
const zip = new PizZip(fs.readFileSync(value));
const doc = new Docxtemplater(zip, {
paragraphLoop: true,
parser,
linebreaks: true,
});
doc.render(scope);
}
return value;
},
};
}
const doc = new Docxtemplater(zip, {
modules: [new SubtemplateModule({})],
parser,
});
doc.render({
docs: ["doc1.docx", "doc2.docx"],
});
When importing a document, the imported document will normally inherit styles from the main document.
For example, if the main document defines the style "Heading1" as purple with a font family of Courrier New, all "Heading1" that come from the included document will also be purple with Courrier new.
Since version 3.17.0, there is a new mode called keepStyles
. With this mode, all styles defined in the subtemplate will be renamed if necessary so that the documents are added with the style of the included document.
Use this option like this :
const SubtemplateModule = require("docxtemplater-subtemplate-module");
const fs = require("fs");
const headerZip = new PizZip(fs.readFileSync("header.docx"));
const footerZip = new PizZip(fs.readFileSync("footer.docx"));
const headerDoc = new Docxtemplater().loadZip(headerZip);
headerDoc.keepStyles = true;
const footerDoc = new Docxtemplater().loadZip(footerZip);
footerDoc.keepStyles = true;
const zip = new PizZip(fs.readFileSync("template.docx"));
const doc = new Docxtemplater(zip, {
modules: [new SubtemplateModule({})],
});
doc.render({
subtemplate: headerDoc,
footer: footerDoc,
});
It is possible to include subsections, however, if you only have access to the subtemplate module, the headers will not be included.
If you have access to the subsection module, you can do the following to also include the headers and footers of the included document :
const SubtemplateModule = require("docxtemplater-subtemplate-module");
const SubSectionModule = require("docxtemplater-subsection-module");
const doc = new Docxtemplater(zip, {
modules: [
new SubtemplateModule({
subsection: new SubSectionModule(),
}),
],
});
doc.render(data);
When using the SubSectionModule with the SubtemplateModule, by default, the section where your tag {:include doc}
is kept, meaning that it will create a pagebreak before adding the document, and also at the end of the included document, it will create a pagebreak.
If you would prefer to not have this pagebreak, than the currentsubsection of the document is going to be replaced by the one from the subtemplate. To do this, you have two attributes that you can set on the included document, replaceFirstSection
and replaceLastSection
:
const fs = require("fs");
const includedContent = fs.readFileSync("included.docx");
const includedDoc = new Docxtemplater(
new PizZip(includedContent)
);
includedDoc.replaceFirstSection = true;
includedDoc.replaceLastSection = true;
doc.render({ included: includedDoc });
The SegmentModule is part of the same package, and allows you to define a "segment" in the document that can then be included multiple times in the document.
You can use it like this in your template :
In your code, write like this :
const SegmentModule =
require("docxtemplater-subtemplate-module").Segment;
const doc = new Docxtemplater(zip, {
modules: [new SegmentModule()],
});
doc.render(data);
When you use segments, you can use the {:includesegment* segment.value}
to include a segment dynamically.
For example, with following data :
doc.render({
parts: ["part2", "part2", "part1"],
});
You can first define your segments in your code, and then use the {includesegment* .}
to include the segments in the order you want them to be.
Your docx will look like this :
This will use the value from the array parts
to include the segment part2
(twice), and then the segment part1
.
Bugfix when using together with meta module, the following would not work correctly :
(This also needs the meta module version 3.12.0 or higher).
const DocxtemplaterMetaModule = require("docxtemplater-meta-module");
const SubtemplateModule = require("docxtemplater-subtemplate-module");
const metaModule = new DocxtemplaterMetaModule();
const doc = new Docxtemplater(zip, {
modules: [metaModule, new SubtemplateModule()],
});
metaModule.restrictEdition({
password: "test123",
salt: "mHQ9f0jyV8snSWoFHQwZigBo",
});
doc.render(/* data */);
This now works correctly with the subtemplate module.
It also needs the latest version of docxtemplater, 3.51.0.
Bugfix when using keepStyles
option, the docDefaults from the included document are now taken into account.
Correctly copy over colors for colors defined in the style.xml as themeColors.
Bugfix when using following template :
Previously, the headers / footers would be overwritten by the "doc2" element.
Now, the headers and footers are correctly replaced in this case.
Make it possible to use prefix from the constructor
Add typescript typings to be able to change the module prefix
Please upgrade both to the latest version of docxtemplater and also to the latest version of the subsection module when you upgrade to this version.
Currently, those are : docxtemplater@3.47.2 and docxtemplater-subsection-module@3.5.8
When used together with the subsection module, the following behavior was happening in previous versions :
When using the Subtemplate and Subsection mode together, if your master document defined multiple headers, one for the first page and one for the other pages, and you include a subdocument somewhere in your document, the section after the included document would now use the first page header. Instead, it now uses the default header.
Many improvements to the keepStyles
, it fixes multiple bugs for :
Add support for keepStyles
option, which will is a mode where the document included will not inherit the styles from the main document.
When updating to this version, please also update your docxtemplater library to at least docxtemplater@3.46.2
This new version fixes a rare bug when including a document which itself is including another document containing a chart, and which resulted in a corrupt document.
Bugfix when working with the subsection module.
In previous versions, when using the SubtemplateModule with a wrapped SubsectionModule, like this :
const SubtemplateModule = require("docxtemplater-subtemplate-module");
const SubSectionModule = require("docxtemplater-subsection-module");
const doc = new Docxtemplater(zip, {
modules: [
new SubtemplateModule({
subsection: new SubSectionModule(),
}),
],
});
doc.render(data);
Right after the {:include doc}
tag, the footer and header used after the included doc is present is the same header/footer as the one from the included doc. However, it should after the {:include doc}
tag, use the header/footer from the main document that was specified before the {:include}
tag.
Upgrade module to use NodeNext moduleResolution setting. See explanation here
Bugfix when an included document contains a "Microsoft visio" file.
In previous versions, the output document would be corrupted, now it works well.
Use new resolvedId
API => requires docxtemplater@3.43.0
Bugfix when including :
Now, the document will be generated correctly (without any corruption).
Throw specific error if trying to include a subtemplate that is of "pptx" fileType
Previously the stacktrace shown was :
When trying to include a document that has no styles.xml or no settings.xml file, the module would throw an error.
Now, the document is included correctly.
Allow to insert emf images (use correct content-type of image/x-emf)
Bugfix when included contains Target=NULL
Fix bug when using renderAsync and parser compilation fails : the error should now be shown inside a multierror. To to this, we know use sm.getValueAsync
in resolve instead of sm.getValue
Bugfix to set or get xml attributes correctly, even when using tabs instead of spaces in tags attributes
This fixes a corruption when including a document that has tabs instead of spaces in its attributes.
Set module.priority in order to have no more issues related to module ordering
Bugfix when used together with HTML module, when the document contains a comment + a {~~html} tag
The following error was thrown :
Now this error is fixed.
Bugfix, when using async mode and having empty data, following error was thrown :
Now, no more error is thrown.
In some cases, when including a file that contained a chart, following stacktrace would appear :
This error is now fixed, the template is correctly generated.
Allow to include files that have images in <v:imagedata>
tags.
Previously, the image would not be imported correctly.
Improve support when having continuous sectionbreaks.
In previous versions, continuous sectionbreaks would sometimes be replaced by page section breaks, which completely broke some templates.
Upgrade to correctly pass in fileType, contentType, … to postparse, subRender and subResolve
Makes it compatible with docxtemplater 3.40.1
Bugfix for segment module when inside section changes.
Previously, the following error was thrown :
Now the generation works well.
Add module.clone() internal to make module work well with subsegment module
Correctly render tags that are present in comments. (Needs docxtemplater@3.39.0)
Small bugfix when using Segment module with Image and HTML module in async mode.
Improve support for renderAsync when using together with Segment module.
Enhance Segment module to use the correct scope, and make segment module work well even when it contains loops.
Add support for {:subsegment* segment.value}
tag
Add support to use the segment module inside the subtemplate module.
This allows you to define a segment in a subtemplate, and then use the segment from the main template.
Bugfix segment module error message :
The subtemplate module handles only docx filetype, not pptx
This now won't show up (the segment module should be ignored in pptx).
Bugfix segment module.
This fixes a bug of the Segmentmodule (part of the subtemplate module) where the segment would show wrongly an error of "Unclosed loop" when using the segment module with following template :
{:segment s}
{#loop}{#loop}{value} {/}{/}
{:segment/}
{#loop}
{:includesegment s}
{/}
This version needs version 3.37.7 of docxtemplater core.
Bugfix on numbering items when including a subtemplate.
Previously, the color and style of the bullets would sometimes use the style from the included template.
Now the style of the bullets (if the same paragraphstyle is used), should use the style of the master document.
Bugfix when using the segment module and the subsegment contains a raw tag, such as this :
{:segment abc}
{@x = y + z }
{x}
{:segment/}
Now, this will work without any error.
Avoid adding styles with w:default="1"
set.
Correctly handle insertion of external styles.
Avoid changing the "headerFromTop" property of the page when including a document.
Bugfix corruption when footnotes are included and some footnotes are referenced from a table, and some footnotes are referenced from a paragraph.
Bugfix corruption when footnotes are in the main document and in the subtemplate.
Bugfix to work well when having footnotes or endnotes containing images or links.
Reuse the same extension when including files containing images :
If including a "jpeg" file, it will also copy that file as "jpeg", if it is "png" it will copy as "png", …
Add support for including subtemplates that contain SVG images
Add async support for segment module
Update typing file to allow subsection inclusion.
Update to work well with docxtemplater 3.31.0
Avoid error when used together with subsection module
Make module work well in the browser without using xmldom (using window.XMLSerializer)
Automatically convert colors that use "accent1", "accent2", …
This means that if your main document and included document have different themeColors, the color used by the output document will be the ones from the main document.
Bugfix to be able to include 3 subtemplates that each contain a chart.
Bugfix when including multiple subtemplates that each have charts.
Add support to include document that contains footnotes or endnotes.
When using the {:include doc}
tag from a header or a footer, the section breaks inside of the included documents are dropped.
This was causing a corruption in previous versions.
Correctly change all "basedOn", "next", and "link" styles when including a document.
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.
Make module compatible with docxtemplater@3.27.0. Please make sure to update docxtemplater to 3.27.0 at the same time you update this module
Bugfix to have the subtemplate work with Office 365 files (files which use word/document2.xml instead of word/document.xml).
The module works in all possible cases now :
Add Subtemplate.Segment module, to allow to define a segment in a document and include it (in the same document).
Use @xmldom/xmldom instead of xmldom, see this github issue
When including a document that has subsections, the header / footer of the main document should be preserved in all cases.
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.
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 {:subtemplate doc}
, the tag would not be replaced correctly.
Calculate maxDocPrId lazily (only at the time when we need the value), to avoid overhead when attaching the module.
To better match with the general behavior of docxtemplater, if some subtemplate is null or undefined, nothing will be rendered instead of getting the error : "The subtemplate for the tag 'foobar' does not exist"
.
This is now the same as with the loop module and the image module for example.
If you give a value that is not null or undefined but is not a valid Docxtemplater document, an error will still be shown.
Continuation of 3.5.5. There was still one case that would throw Cannot read property 'asArrayBuffer' of undefined
when using inline images (ones present within a paragraph, inside the content).
The error should no more occur now.
Do not fail with error Cannot read property 'asArrayBuffer' of undefined
when having a docx with image (containing absolute Target for images, which might have been generated by some other docxtemplater module or some other program).
Also import correct xmlns
attributes from imported document.
Declare supportedFileTypes, which allows to use this module with the new docxtemplater constructor which was introduced in docxtemplater 3.17.
Bugfix "Cannot read property 'appendChild' of undefined" when including a document where the {:include tag} is followed by a page break
Make it possible to have pagebreaks between two includes, without removing the pagebreak.
Fix issue Cannot read property 'getAttribute' of undefined
when having unnamed styles in main document.
Add numId from styles.xml so that numbering values are better copied
Include w:sdt tags in addition of w:p
tags
Update browser build to use XMLSerializer instead of xmldom
Use requiredAPIVersion
Explanation : On some versions of npm (notably 5.8.0), when having a package containing docxtemplater-subtemplate-module, the installation will generate a tree of node_modules that puts the module on a level where it has no access to docxtemplater. By explicitly asking it as a dependency, this issue is avoided.
Fix bug when using equations and including templates 2 levels deep (with parent.docx /son.docx and grand-son.docx which contains an equation).
Explanation : Recently, the scopemananger API (internal API) has changed, this new version of the subtemplate module makes the module work with both versions newer than 3.6 and older than 3.6 of docxtemplater.
Add support for adding arrays of subtemplates with subsections.
When using the SubsectionModule, make it possible to replace the first section of the including document and replace the last section of the included document (this avoids additional pagebreaks).
Add support for adding subsections with headers by using subsection module.
Add possibility to add sections from subdocument if .includeSections is true
Add meta context argument to custom parser with information about the tag for each types of tags
Add support for including documents with pagebreaks
Fix corruption when including equations
Add support for including charts refering to Excel documents
Add support for including documents containing Excel documents (OLEObjects)
Add support for including documents containing comments
Avoid corruption when including multiple charts with subtemplates
Bugfix corruption when including document with chart
Add support for including documents containing charts
Remove error getElementsByTagName when word/numbering.xml doesn't exist in main document
Fix bug when subtemplate contains list coming from an other module (HTML)
Images in the subtemplate when there are already some images in the headers does not cause a corruption anymore.
Links in the subtemplate are now handled correctly
Fix corruption on some word version because of duplicate styles
Add support for lists in subtemplates (ordered lists and unordered lists).
Import styles in the case when the included document defines some styles.
Handle images importing
Initial release