guigrpa / docx-templates

Template-based docx report creation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error executing command 'x': document is not defined

rhys-phelps-labs opened this issue · comments

Am trying to use docx-templater in supabase edge function (Deno). But I dont seem to be able to get the library to work?

Error: Error executing command 'quote_number': document is not defined
at runUserJsAndGetRaw (https://unpkg.com/docx-templates@4.11.1/lib/browser.js:26:224)
at processCmd (https://unpkg.com/docx-templates@4.11.1/lib/browser.js:26:6387)
at processText (https://unpkg.com/docx-templates@4.11.1/lib/browser.js:26:5561)
at walkTemplate (https://unpkg.com/docx-templates@4.11.1/lib/browser.js:26:4592)

The template is a docx file, with simple direct tags:
{quote_number}, {quote_name}.
There is no Query, EXEC or INIT anywhere in the file. As I prepare the data before hand and then pass it to createReport.

const quote_data = await req.json();
let template:Blob = data;
const buffer = await createReport({
  template,
  quote_data,
  cmdDelimiter:  ['{','}'],
  noSandBox: true
});

Attached is the template and the dummay data used for testing.
oa_quote_template.1.0.docx
dummy_data.txt

Other information:

  • This is the first time using docx-templates on Supabase Edge Functions.
  • We are using the Free cloud hosted supabase dev setup to perform the test. (Its not a local development environment)

any help is always appreciated.

Cheers

createReport expects an arguments object with the data property. You're providing an object with the quote_data property, which docx-templates does not use and is likely a mistake.

You probably want to write:

const quote_data = await req.json();
let template:Blob = data;
const buffer = await createReport({
  template,
  data: quote_data,
  cmdDelimiter:  ['{','}'],
  noSandBox: true
});

Lmk if this does not fix your issue.

Recommend adding strict tsconfig and eslint rules so you can rely on intellisense more to avoid stuff like this.

Sorry, I am struggling to get this working. I am not very experienced with Typescript.

Have been trying different combinations of the following:

"docx-templates": "https://unpkg.com/docx-templates/lib/browser.js",

const buffer = await createReport({
      template,
      data: { 
        quote_number: '1234',
        quote_date: '2023-04-18',
      },
      cmdDelimiter:  ['{','}']
    });

and still I get same result.
"Error executing command 'quote_number': document is not defined"

@rhys-phelps-labs Eyeballing your code, I think the vm polyfill does not behave as expected in your deno environment. I think it expects the document global to be available, like it would be in a browser environment.

You can try disabling sandbox mode by setting noSandbox: true. This will run the code of each command in a simple eval call rather than a vm sandbox.

You should only disable the sandbox if you understand the security implications. See https://github.com/guigrpa/docx-templates#performance--security. If you disable sandboxing, make sure you trust your template; think of your template as JS code (it actually is JS code). Don't let users upload template.

Let me know if this solves your problem.

Begging your forgiveness. I spelt noSandBox instead of noSandbox.

Thankyou, your library is a life saver.