typescriptdocxtemplater

I am using Docxtemplater to apply an image to docx file but it return error:"Cannot read properties of undefined (reading 'part')"


const content = fs.readFileSync(
    path.resolve(__dirname, "../Template/quote2.docx"),
    "binary"
);

// console.log(content);

const imageOpts = {
    centered: false,
    fileType: "docx",
    getImage: function (tagValue, tagName) {
        return fs.readFileSync(tagValue);
    },
    getSize: function (img, tagValue, tagName) {
        // it also is possible to return a size in centimeters, like this : return [ "2cm", "3cm" ];
        return [150, 150];
    },
};

const zip = new PizZip(content);
const doc = new Docxtemplater(zip, {
    modules: [new ImageModule(imageOpts)],
});

doc.setData({image: './image.png'});

doc.render();

const buffer = doc.getZip().generate({
    type: "nodebuffer",
    compression: "DEFLATE",
});

fs.writeFile("test.docx", buffer);

Error:

F:\Document_D\Learn_Node_JS\Project1\server\node_modules\docxtemplater\js\scope-manager.js:56
  if (!this.cachedParsers || !meta.part) {
                                   ^

TypeError: Cannot read properties of undefined (reading 'part')
    at ScopeManager._getValue (F:\Document_D\Learn_Node_JS\Project1\server\node_modules\docxtemplater\js\scope-manager.js:56:36)
    at ScopeManager.getValue (F:\Document_D\Learn_Node_JS\Project1\server\node_modules\docxtemplater\js\scope-manager.js:188:30)
    at ImageModule.render (F:\Document_D\Learn_Node_JS\Project1\server\node_modules\docxtemplater-image-module\js\index.js:83:40)
    at moduleRender (F:\Document_D\Learn_Node_JS\Project1\server\node_modules\docxtemplater\js\render.js:11:30)
    at F:\Document_D\Learn_Node_JS\Project1\server\node_modules\docxtemplater\js\render.js:33:26
    at Array.map (<anonymous>)
    at render (F:\Document_D\Learn_Node_JS\Project1\server\node_modules\docxtemplater\js\render.js:31:24)
    at XmlTemplater.render (F:\Document_D\Learn_Node_JS\Project1\server\node_modules\docxtemplater\js\xml-templater.js:226:22)
    at F:\Document_D\Learn_Node_JS\Project1\server\node_modules\docxtemplater\js\docxtemplater.js:444:21
    at Array.forEach (<anonymous>)
    at Docxtemplater.render (F:\Document_D\Learn_Node_JS\Project1\server\node_modules\docxtemplater\js\docxtemplater.js:438:32)
    at fill (F:\Document_D\Learn_Node_JS\Project1\server\routes\fill_Template.js:47:9)
    at F:\Document_D\Learn_Node_JS\Project1\server\routes\fill_Template.js:132:5
    at Layer.handle [as handle_request] (F:\Document_D\Learn_Node_JS\Project1\server\node_modules\express\lib\router\layer.js:95:5)
    at next (F:\Document_D\Learn_Node_JS\Project1\server\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (F:\Document_D\Learn_Node_JS\Project1\server\node_modules\express\lib\router\route.js:112:3)

Solution

  • i fixed my problem by using a free-docx-templater-image-module, because the defaul templater-image-module is not free and update any more, so it not compatible with docxtemplater newest version https://www.npmjs.com/package/docxtemplater-image-module-free