This module exposes a tag to replace a qrCode image in your template into any kind of image. The qrcode of the pages are parsed, then they are replaced with with the getImage
function.
Docxtemplater will keep all the styling that you put on your qrcode (shades, effects, size, …).
The following :
with following data :
doc.renderAsync({
company: {
logo: "acme.png",
},
});
Will render :
Your docx should contain an image that contains a qrcode text. You can use The Qrcode Generator in Text mode to generate your qrcodes (you need to click on the "free text" button first).
The file has to be a JPG/BMP/PNG file, since the image parser that we use can only read these formats (TIFF format is not handled). If your qrcode is not replaced, it is possible that it is because your image was saved as TIFF.
After installing the module, you can use a working demo by running node sample.js
.
The qrcode module takes as an argument the getImage
function, which should return the image depending on the resolved tag value.
const QrCodeModule = require("docxtemplater-qrcode-module");
const fs = require("fs");
const path = require("path");
const qrCodeModule = new QrCodeModule({
getImage(value) {
return fs.readFileSync(
path.resolve(__dirname, "..", "images", value)
);
},
});
const doc = new Docxtemplater(zip, {
modules: [qrCodeModule],
});
doc.render(data);
The qrcode module constructor takes as an argument the autoDelimiters
boolean (false by default). When autoDelimiters is set to true, instead of encoding "{image}" in your qrcode, you can just encode "image".
It is possible to get images asynchronously by returning a Promise in the getImage function and use the docxtemplater async api.
To make this module work well in webpack, you have to declare the following in your webpack.config.js
(in the module.exports):
module.exports = {
// ...
plugins: [
new webpack.DefinePlugin({
"process.env": {
ENV: JSON.stringify("BROWSER"),
},
}),
],
};
Bugfix to work correctly with docxtemplater 3.51.0
If using a previous version of the qrcode module, you would see following error :
Bugfix to work correctly with image-module version 3.28.2
Upgrade to use @jimp/custom to avoid pulling many unused dependencies
Upgrade module to use NodeNext moduleResolution setting. See explanation here
When using the inspectModule to getAllTags, avoid showing "undefined" key for qrcode tags.
Requires docxtemplater 3.42.1 or higher
Allow to insert emf images (use correct content-type of image/x-emf)
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
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
Add endLindex to qrcode part.
Fixes issue of disappearing images when used together with subsegment module
This version requires docxtemplater@3.35.0 or more.
Bugfix : When having an image inside a textbox that also had some text, the tags that are in the textbox were not correctly replaced.
This happened both if the image was a real qrcode or if it was a standard image tag.
The bug meant that the tags were replaced like this :
{first_name} {last_name}
With the data : doc.render({first_name: "John", last_name: "Doe"});
Was replaced by this :
first_name last_name
Rename loader function to getImage
The loader function can no more be used now.
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/qrcode-module.js
The .min.js file is also created now.
Add typescript definitions for public API
Declare supportedFileTypes, which allows to use this module with the new docxtemplater constructor which was introduced in docxtemplater 3.17.
Update browser build to use XMLSerializer instead of xmldom
Use requiredAPIVersion
Update jsqr to 1.1.1
Fix bug docPrId duplicated
Explanation : On some versions of npm (notably 5.8.0), when having a package containing docxtemplater-qrcode-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 qrcode module makes the module work with both versions newer than 3.6 and older than 3.6 of docxtemplater.
Add meta context argument to custom parser with information about the tag for each types of tags
Promise
in the getImage
function).Accept getImage
as option to have similar API as the image module.
loader
is still accepted, but will be deprecated in future major version
the qrcode could be decoded to {parameters}
and the data is :
{
"parameters": { "path": "path_to_file.png" }
}
and the loader
function will now be called with :
loader({"path": "path_to_file.png"})
before this release, it was called with "" by mistake.
Initial release