open-xml-templating/docxtemplater

subtemplate error: tagValue.zip.file is not a function

wcordelo opened this issue · 5 comments

Environment

  • Version of docxtemplater : ^3.31.0
  • Used docxtemplater-modules : docxtemplater-subtemplate-module
  • Runner : Node.JS

How to reproduce my problem :

With the following js file :

    try {
      const { template, type, payload } = r;
      let file;
      let documentPayload = payload && getDocumentPayload(payload);
      // Create a zip file based on type
      const zip = new pizzip(template, { base64: false });
      const fixDocPrCorruptionModule = require("docxtemplater/js/modules/fix-doc-pr-corruption");
      const docxTemplater = new Docxtemplater(zip, {
        parser: angularParser,
        nullGetter: nullGetter,
        modules: [new SubtemplateModule({}), fixDocPrCorruptionModule],
        errorLogging: true,
      });
      if (type === DocxtemplaterType.MainTemplate) {
        if (!!documentPayload) {
          documentPayload = await getPayloadWithSubtemplates(documentPayload); // calls the sub-template portion on the bottom
          docxTemplater.render(documentPayload);
        } else {
          docxTemplater.render();
        }
        file = docxTemplater;
      } else if (type === DocxtemplaterType.SubTemplate) {
        if (!!documentPayload) {
          docxTemplater.render(documentPayload);
        } else {
          docxTemplater.render();
        }
        file = new Docxtemplater().loadZip(docxTemplater.getZip());
      } else {
        file = zip;
      }
      return {
        statusCode: StatusCode.OK,
        payload: file
      };
    } catch (error) {
      console.log('err: ', error);
      return {
        statusCode: StatusCode.Failed,
        payload: {
          description: JSON.stringify(error),
          error: "failed to generate document",
        }
      };
    }

I would expect it to :

  • not fail (include error message):
tagValue.zip.file is not a function
at SubtemplateModule.addDocument (/workspace/node_modules/docxtemplater-subtemplate-module/js/index.js:489:35)
at SubtemplateModule.render (/workspace/node_modules/docxtemplater-subtemplate-module/js/index.js:1042:23)
at moduleRender (/workspace/node_modules/docxtemplater/js/render.js:9:30)
at /workspace/node_modules/docxtemplater/js/render.js:26:26
at Array.map (<anonymous>)
at render (/workspace/node_modules/docxtemplater/js/render.js:24:24)
at XmlTemplater.render (/workspace/node_modules/docxtemplater/js/xml-templater.js:198:22)
at /workspace/node_modules/docxtemplater/js/docxtemplater.js:380:21
at Array.forEach (<anonymous>)
at Docxtemplater.render (/workspace/node_modules/docxtemplater/js/docxtemplater.js:374:32)

@edi9999 also running into another issue with zip files not loading properly as subtemplates. Happens sometimes, not always.

The filetype for this file could not be identified, is this file corrupted ?
at new XTInternalError (/workspace/node_modules/docxtemplater/js/errors.js:42:16)
at throwFileTypeNotIdentified (/workspace/node_modules/docxtemplater/js/errors.js:335:13)
at Docxtemplater.updateFileTypeConfig (/workspace/node_modules/docxtemplater/js/docxtemplater.js:338:9)
at Docxtemplater.loadZip (/workspace/node_modules/docxtemplater/js/docxtemplater.js:191:12)
at new Docxtemplater (/workspace/node_modules/docxtemplater/js/docxtemplater.js:61:12)

Is there a way to enforce or set a filetype to prevent this issue?

Hello @wcordelo , interesting.

Can you please update to the latest docxtemplater version and module versions ?

You can do so using following command :

npm install docxtemplater && npx -y update-docxtemplater && npm install

There were some quite recent fixes related to docm files, so maybe some of the errors that you get can be explained.

If you're already up to date, I don't see why this would behave like this, are you sure that your input files are correct ?
The best would probably to record the input that you have and send it to me.

If the error message The filetype for this file could not be identified, is this file corrupted ? appears, it means that the file is a valid zip file, however it is most likely not a docx, pptx, docm, pptm file.

If the message appears : tagValue.zip.file is not a function, it means that the tagValue does not contain a correct zip file.

Did you get further on this issue @wcordelo ?

@edi9999 I'm going to work on this issue this week. I'll provide some updates soon.

Hi @wcordelo , I'm going to close this issue but feel free to comment on it, I will see it.

Or hopefully your bug has been fixed automatically.

Have a nice weekend