guigrpa / docx-templates

Template-based docx report creation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The "createReport" function Error: Can't read the data of 'the loaded zip file'

gjlong opened this issue · comments

commented

Dear author,
I used the "createReport" function in my code.
But the console print the error when the code run.

The Error is:
Error: Can't read the data of 'the loaded zip file'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?

code:
const tempDoc=fs.readFileSync('/test/example.docx')
const tty=async()=>{
try{
const reportb=await createReport({
tempDoc,
data:{
name:'tom'
},
});
}catch(error){
console.log(error)
}
}
tty()

project version:
The docx-templates version is the latest by npm install.

I find some Issues about this error in the docx-templates project,but it can't fixed the error.
Can you tell me where is the wrong in my program and how to fix it?
Thank you.

Yes, you are not passing a template. Note that createReport expects an object with a property called template. You passed an object with the property tempDoc instead.

You should study javascript object shorthand syntax, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#property_definitions

        const reportb = await createReport({
            template: tempDoc,
            data: {
                name: 'tom'
            },
        });
commented

Yes, you are not passing a template. Note that createReport expects an object with a property called template. You passed an object with the property tempDoc instead.

You should study javascript object shorthand syntax, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer#property_definitions

        const reportb = await createReport({
            template: tempDoc,
            data: {
                name: 'tom'
            },
        });

Thanks for your suggestion!
I tried to add this 'template' when I created this issue ago.
But the problem is still unsolved.

code:
async function tty() {
try {
const reportb = await createReport({
template: tempDoc,
data: {
name: 'Tom'
},
});
} catch (error) {
console.log(error)
}
}
tty();

I don't know how can fixed it by another way.

@gjlong
Please use markdown code blocks when you post code snippets in github issues.

The 'Can't read the data of 'the loaded zip file'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?' error means that the template you are providing is not a valid zip file (.docx files are zip files).

You should double-check whether the docx file you are using as a template is valid.

Also, I don't know what environment you are using so it is impossible for me to tell what your implementation of fs.readFileSync actually does. Make sure it produces a Buffer (when using NodeJS).

The below works for me without problems.

import { createReport } from "docx-templates";
import fs from 'fs'

async function tty() {
    try {
        const tempDoc = fs.readFileSync('./example.docx')
        const reportb = await createReport({
            template: tempDoc,
            data: {
                name: 'Tom'
            },
        });
        console.log('result:', reportb)
    } catch (error) {
        console.log(error)
    }
}
tty();
commented

@gjlong Please use markdown code blocks when you post code snippets in github issues.

The 'Can't read the data of 'the loaded zip file'. Is it in a supported JavaScript type (String, Blob, ArrayBuffer, etc) ?' error means that the template you are providing is not a valid zip file (.docx files are zip files).

You should double-check whether the docx file you are using as a template is valid.

Also, I don't know what environment you are using so it is impossible for me to tell what your implementation of fs.readFileSync actually does. Make sure it produces a Buffer (when using NodeJS).

The below works for me without problems.

import { createReport } from "docx-templates";
import fs from 'fs'

async function tty() {
    try {
        const tempDoc = fs.readFileSync('./example.docx')
        const reportb = await createReport({
            template: tempDoc,
            data: {
                name: 'Tom'
            },
        });
        console.log('result:', reportb)
    } catch (error) {
        console.log(error)
    }
}
tty();

The Github add code is not working in the browser.

This example code is running in the weixin mini program.
The weixin mini program environment is javascript core and v8,not NodeJS.
So I think the Buffer is not as same as in the NodeJS.
And the example code can run in the browser environment.
I think your opinion is right.