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.37k stars 484 forks source link

patchDocument failing #2321

Open aylusltd opened 1 year ago

aylusltd commented 1 year ago

So simple reproduction:

    const f = fs.readFileSync(path, {encoding: null})
    const patchedDoc = await patchDocument(u, patch_opts)

TypeError [ERR_INVALID_ARG_TYPE]: The "chunk" argument must be of type string or an instance of Buffer or Uint8Array. Received type number (80)

I've confirmed path is valid, and can be opened by the process. console.log(f.length) prints the file length in bytes.

for reference:

const patch_opts:PatchDocumentOptions = { patches: {
                velocity_graph : {
                    type: PatchType.PARAGRAPH,
                    children: [
                        new ImageRun({
                            data: await makeImage({
                                url:`${clientBaseUrl}graph/${companyId}/Velocity`,
                                token_user,
                                expectedWidth: 1000,
                                expectedHeight: 415,
                                expectedTop: 30
                            }
                            ),
                            transformation: {
                                width: 1000,
                                height: 415
                            }
                        })
                    ]
                }

Running on: Node v18.14.2 Ubuntu 22.04

aylusltd commented 1 year ago

The file is a two line docx. Confirmed it opens in word. Contents is

    This is a demonstration template
    {{velocity_graph}}

No formatting is applied.

aylusltd commented 1 year ago

Should have included:

"node_modules/docx": {
         "version": "8.2.2",
         "resolved": "https://registry.npmjs.org/docx/-/docx-8.2.2.tgz",
         "integrity": "sha512-dWI5WfD/fDCLdjIA7CcDzV/1uyBD+mmr7jDLTUN997hcbPz56E701Kf1EEWdibdH9kk+0tFSmE+C0jTlkRZ7kQ==",
         "dependencies": {
            "@types/node": "^20.3.1",
            "jszip": "^3.10.1",
            "nanoid": "^4.0.2",
            "xml": "^1.0.1",
            "xml-js": "^1.6.8"
         },
         "engines": {
            "node": ">=10"
         }
      },

And since the bug is likely in jszip:

"node_modules/jszip": {
         "version": "3.10.1",
         "resolved": "https://registry.npmjs.org/jszip/-/jszip-3.10.1.tgz",
         "integrity": "sha512-xXDvecyTpGLrqFrvkrUSoxxfJI5AH7U8zxxtVclpsUtMCq4JQ290LY8AW5c7Ggnr/Y/oK+bQMbqK2qmtk3pN4g==",
         "dependencies": {
            "lie": "~3.3.0",
            "pako": "~1.0.2",
            "readable-stream": "~2.3.6",
            "setimmediate": "^1.0.5"
         }
      },
aylusltd commented 1 year ago

I was able to create a working local version of patchDocument by replacing JSZip with adm-zip and making some other minor alterations to the files. Unfortunately I can't make a PR with what I have. Will try to do it next week.

dolanmiu commented 1 year ago

interesting, does regular document creation work for you? that uses jszip too

SpraxDev commented 1 year ago

Having some issues in Deno with jszip too (#1578) and adm-zip looks kinda nice. Maybe replacing the lib can fix both issues? 😄

aylusltd commented 11 months ago

@dolanmiu dunno, we never actually do that. We started with a word template and had to update. We wound up switching just to straight pdf generation though.