Important : This module only supports pptx (Powerpoint documents), not docx, see the html if you want to include HTML inside Word documents.
This module currently supports:
<p>
<h1-h6>
with some default sizes for each level, the sizes of the titles can be customized<span>
<br>
<ul>
, <ol>
and <li>
for ordered and unordered lists<strong>
and <em>
<a href="URL">Linktext</a>
<table>
, <tr>
, <tbody>
, …To insert HTML :
~
is used for inline HTML, such as : {~html}
or {~inlineComment} which will use the "inlineComment" data~~
is used for block HTML, such as : {~~html}
or {~~styledParagraphs} which will use the "styledParagraphs" data~~~
is used for shape HTML, such as : {~~~html}
or {~~~styledTable} which will use the "styledTable" dataconst HTMLPptxModule = require("docxtemplater-html-pptx-module");
const fs = require("fs");
const doc = new Docxtemplater(zip, {
modules: [new HTMLPptxModule({})],
});
doc.render({ html: "<b>Hello</b>, Foo !" });
const buffer = doc.getZip().generate({
type: "nodebuffer",
compression: "DEFLATE",
});
fs.writeFile("test.docx", buffer);
<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/html-module.js"></script>
<script>
PizZipUtils.getBinaryContent(
"examples/input.docx",
function (error, content) {
if (error) {
console.error(error);
return;
}
const zip = new PizZip(content);
const doc = new docxtemplater(zip, {
modules: [
new DocxtemplaterPptxHtmlModule({}),
],
});
doc.render({
html: "<p>Hello <b>John</b></p>",
});
const out = doc.getZip().generate({
type: "blob",
mimeType:
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
});
saveAs(out, "generated.docx");
}
);
</script>
</html>
It is not possible to mix different types of HTML elements in a single shape.
In the PPTX format, everything is inside a "box" which is techically called a "shape" (XML tag <p:sp>
) , and a box can contain either :
The reason for that is that each box is statically positioned (x and y coordinate are set in the powerpoint document), so it is not possible to place a table and an image on the same tag for example because we do not know where we should place the second element.
In a pptx template, the tag {~~html} will be inside one particular box, so it can't generate multiple boxes (or they would overlap, which would be unreadable).
This is why one HTML tag : {~~html} will alway render one box, so for example the following HTML data would throw an error because it mixes tables and paragraphs :
<p>Hello</p>
<table>
<tr>
<td><p>Hello</p></td>
</tr>
</table>
<p>Hello</p>
To insert a table, you'll have to use the "shape" level HTML tag, with three "tilde" symbols, like this :
In your code, you can then write :
doc.render({
myTable: `
<table>
<tr>
<td><p>Hello</p></td>
</tr>
</table>
`,
});
It is possible to set options to the module in its constructor.
The options are as follows :
<span>
;To ignore all unknown tags:
const HTMLPptxModule = require("docxtemplater-html-pptx-module");
const doc = new Docxtemplater(zip, {
modules: [
new HTMLPptxModule({
ignoreUnknownTags: true,
}),
],
});
doc.render(/* data */);
Add support for text-decoration-color (on normal text or links) to customize the underline color.
Improve support for star selector.
Make it possible to use prefix from the constructor
Add support for :
<mark>
tag to highlight text in yellowbackground-color
set on textnullGetter
if the value of the data is nullishBugfix of a corruption, when using a {~html}
tag that itself is a link, the output document could get corrupted.
It now works correctly.
Add typescript typings to be able to change the module prefix
When having a loop with an inline HTML tag inside it, the following error would sometimes appear :
This error should no more appear, the document should generate correctly now.
Allow to change text color of links using following cod :
const HTMLPptxModule = require("docxtemplater-html-pptx-module");
const doc = new Docxtemplater(zip, {
modules: [
new HTMLPptxModule({
styleSheet: `
a {
color: #f3a;
}
`,
}),
],
});
doc.render({
html: '<a href="https://docxtemplater.com">link</a>',
});
Fix regression : correctly template slideMaster and slideLayout files (if some {placeholders} are present in the slideMaster, those will now be correctly templated).
Add support for vertical-align: bottom
(or middle
, top
).
See this Create React App issue and this @adobe/css-tools issue.
Add support for <figure>
and <figcaption>
tag (which renders exactly like a div).
Upgrade module to use NodeNext moduleResolution setting. See explanation here
Bugfix to set or get xml attributes correctly, even when using tabs instead of spaces in tags attributes
Set module.priority in order to have no more issues related to module ordering
Add initial support for tables.
To add a table, you need to use a tag with three ~, like this :
const HTMLPptxModule = require("docxtemplater-html-pptx-module");
const doc = new Docxtemplater(zip, {
modules: [
new HTMLPptxModule({
styleSheet: `
tr td,tr th {
border-top: none;
border-left: 6px solid orange;
border-bottom: 4px dashed orange;
}
`,
}),
],
});
doc.render({
html: `
<table>
<tr>
<td>Hello</td>
<td>Foo</td>
</tr>
</table>
`,
});
This initial version supports the following :
colspan
and rowspan
background-color
border
using the stylesheet optionAdd module.clone() internal to make module work well with subsegment module
Bugfix issue when having {~~html} as the title :
The following error was thrown :
Now, the generation happens (no stacktrace thrown).
Add support for margin-left or padding-left on paragraph element.
Correctly keep font-family of existing text used in the {~~html} tag.
Previously, the font-family was lost.
Add support for inline html tag using {~html}
(block tag is with {~~html}
).
Rename property this.prefix
to this.blockPrefix
.
Bugfix "Cannot read properties of undefined (reading 'forEach')", when using stylesheet with comments.
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.
Bugfix issue : TypeError: Cannot read properties of undefined (reading 'getElementsByTagName')
in `getSlideLayoutPath
when using together with the slides 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/html-module.js
The .min.js file is also created now.
Add typescript definitions for public API
When specifying a color with CSS, in previous versions, if the text containing the {~~html} tag was colored with a theme, the document could be corrupted.
This release fixes the bug, the color is now the right one.
Better handle override of style. Includes a quite big rewrite.
For example, it is now possible to override the color of some text in a given run without producing a corruption and without losing all other formatting on that block of text.
Disallow to pass something other than a string to the stylesheet option (because this option only allows to pass a given global stylesheet)
Bugfix when using together with slides module, it would result in the following error :
Now, loops created with the slides module can contain html-pptx tags
Fix issue with css module :
Now, the module requires only the part that it uses, which removes this error.
Bugfix to ignore <br>
at the end of block elements.
Bugfix to render space in inline HTML tag when using : <b>Foo</b> <i>Bar</i>
Bugfix corruption when using ul and li, sometimes, it would return a float value for spcPts
Add way to specify spacing between bullet and text, and indent of bullet with :
ul li {
-dxt-bullet-indent: 1in;
-dxt-bullet-spacing: 1in;
}
Better handle margin-top/margin-bottom for li tags
Declare supportedFileTypes, which allows to use this module with the new docxtemplater constructor which was introduced in docxtemplater 3.17.
Add support for <u>
, <sub>
and <sup>
tag and for style : text-decoration: underlined
.
Initial release