dolanmiu / docx

Easily generate and modify .docx files with JS/TS with a nice declarative API. Works for Node and on the Browser.
https://docx.js.org/
MIT License
4.41k stars 486 forks source link

Patcher is throwing a "Token not found" error seemingly randomly in a simple document #2706

Closed alandsidel closed 3 weeks ago

alandsidel commented 3 months ago

Been beating my head against the wall for a few hours here, can't understand what might be causing this issue.

I have a simple docx file taken from a larger file which has a few paragraphs containing mustache formatted tokens. When using the patcher only about half of the tokens can be replaced while the rest result in a "Token not found" error and I cannot find any rhyme or reason to why some work and some don't. I've tried changing their names, I've made the tokens all lower case rather than mixed case, changed the order that I give them to the patcher, etc. Nothing seems to work.

The code I'm using in the patcher for testing is as follows, I've cut it down as well to just the minimum needed to cause the issue. resp.data is the raw document (a blob from an axios get). If any of the commented sections are uncommented, the error occurs. I can't see anything wrong in the code or the template document, which I've also cut down for testing and attached.

The error from the console (this is in a React app) is:

Error: Token not found
    at findRunElementIndexWithToken (docx.js?v=ef61ae49:21098:9)
    at replacer (docx.js?v=ef61ae49:21146:23)
    at docx.js?v=ef61ae49:21363:9
    at Generator.next (<anonymous>)
    at fulfilled (docx.js?v=ef61ae49:1691:24)

Any insights would be greatly appreciated. test.docx

patchDocument(resp.data, {
  patches: {
    todays_date: {
      type: PatchType.PARAGRAPH,
      children: [new TextRun({text: (new Date()).toLocaleDateString()})]
    },

    school_name: {
      type: PatchType.PARAGRAPH,
      children: [new TextRun({text: 'test'})]
    },

    //address: {
    //  type: PatchType.PARAGRAPH,
    //  children: [new TextRun({text: 'test'})]
    //},

    //city: {
    //  type: PatchType.PARAGRAPH,
    //  children: [new TextRun({text: 'test'})]
    //},

    //state: {
    //  type: PatchType.PARAGRAPH,
    //  children: [new TextRun({text: 'test'})]
    //},

    //zip: {
    //  type: PatchType.PARAGRAPH,
    //  children: [new TextRun({text: 'test'})]
    //},

    phone: {
      type: PatchType.PARAGRAPH,
      children: [new TextRun({text: 'test'})]
    },

    first_name: {
      type: PatchType.PARAGRAPH,
      children: [new TextRun({text: 'test'})]
    },

    last_name: {
      type: PatchType.PARAGRAPH,
      children: [new TextRun({text: 'test'})]
    },

    //email_address: {
    //  type: PatchType.PARAGRAPH,
    //  children: [new TextRun({text: 'test'})]
    //},

    ft_dates: {
      type: PatchType.PARAGRAPH,
      children: [new TextRun({text: 'test'})]
    },

    grade: {
      type: PatchType.PARAGRAPH,
      children: [new TextRun({text: 'test'})]
    },
  }
}).then((doc) => {
  console.log('doc created');
});
nathanqueija commented 1 month ago

I'm facing something similar. Didi you figure out what was the issue?

alandsidel commented 1 month ago

I'm facing something similar. Didi you figure out what was the issue?

Nope. My solution was to switch to a different library. Using easy-template-x now. Works like a charm.

dolanmiu commented 3 weeks ago

I can reproduce it on my end, will investigate

Example here: https://github.com/dolanmiu/docx/blob/master/demo/93-template-document.ts