This module exposes two tags to import headers of footers from an external Word document, by changing the subsection :
{:subsection doc}
will create a new subsection on the location of the tag. It will use the header and footer that are present in the document doc
.{:replacesection doc}
will replace the current subsection. It will use the header and footer that are present in the document doc
.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.
After installing the module, you can use a working demo by running node sample.js
.
If you want to import headers and footers from document "a.docx" into the main document, do the following :
In your main document, use the tag {:subsection subdoc}
or {:replacesection subdoc}
For example in document-main.docx :
In your code, you can write the following :
const fs = require("fs");
const SubsectionModule = require("docxtemplater-subsection-module");
const doc = new Docxtemplater(
new PizZip(fs.readFileSync("document-main.docx")),
{ modules: new SubsectionModule() }
);
const subdoc = new Docxtemplater().loadZip(
new PizZip(fs.readFileSync("a.docx"))
);
doc.render({
subdoc,
});
At the place where you put the {:replacesection subdoc}
tag, the current section will now use the headers/footers from subdoc (from a.docx
in our example).
You can render the values in your subsection by using a custom parser and render the document with the current scope.
For example :
const SubSectionModule = require("docxtemplater-subsection-module");
const expressionParser = require("docxtemplater/expressions.js");
const doc = new Docxtemplater(zip, {
modules: [new SubSectionModule({})],
parser: function recursiveParser(tagString) {
const parser = expressionParser(tagString);
return {
get(scope, context) {
const module =
context &&
context.meta &&
context.meta.part &&
context.meta.part.module;
const isIncludeTag =
[
"pro-xml-templating/subsection-module",
"pro-xml-templating/replacesection-module",
].indexOf(module) !== -1;
const value = parser.get(scope, context);
if (isIncludeTag && value) {
value.render(scope);
}
return value;
},
};
},
});
doc.render(data);
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 subtemplate module when you upgrade to this version.
Currently, those are : docxtemplater@3.47.2 and docxtemplater-subsection-module@3.17.2
When used together with the subtemplate 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.
Upgrade module to use NodeNext moduleResolution setting. See explanation here
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
Bugfix to avoid adding styles that have w:default="1"
Avoid Error when trying to include an image with a cid target
Avoid corruption related to missing style id.
Make module work well in the browser without using xmldom (using window.XMLSerializer)
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
Use @xmldom/xmldom instead of xmldom, see this github issue
Generate files in built with correct filename In previous versions, the filename was always build/docxtemplater.js
. Now the filename is build/subsection-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 {:subsection 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.
Declare supportedFileTypes, which allows to use this module with the new docxtemplater constructor which was introduced in docxtemplater 3.17.
Bugfix footer appearing multiple times in output
Bugfix footer not correctly included when the XML of the main document is not formatted the exact way xmldom wants it.
Bugfix when including images that are outside /word/media or have absolute paths
Bugfix when including footer or header with links (URLS). The error message was : Cannot read property 'asBinary' of null
Multiple bugfixes :
Bugfix for including footer in separate documents when the header also contains footers.
Bugfix when rId are not numeric
Add support for including headers/footers from separate documents.
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-subsection-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.
Explanation : Recently, the scopemananger API (internal API) has changed, this new version of the subsection module makes the module work with both versions newer than 3.6 and older than 3.6 of docxtemplater.
Make it possible to include the first section of a document having multiple sections.
Add support for being included by subtemplate module
Fix corruption for replacesection tag
Add meta context argument to custom parser with information about the tag for each types of tags
Add {:replacesection} tag that allows to replace a section instead of adding it.
Fix issue with multiple subsection tag in same document
Fix corruption with loop when the headers contain objects such as arrows
Fix corrupted document when the tag {:subsection} is not used in the document
Initial release