Ziv-Barber / officegen

Standalone Office Open XML files (Microsoft Office 2007 and later) generator for Word (docx), PowerPoint (pptx) and Excell (xlsx) in javascript. The output is a stream.
MIT License
2.65k stars 471 forks source link

How can I open the document right after generate #307

Closed woosungchu closed 5 years ago

woosungchu commented 5 years ago

Environment

  1. node -v: [v10.15.3]
  2. npm -v: [6.4.1]
  3. npm ls officegen: [officegen@0.5.2]
  4. Operating system: [Window]
  5. Microsoft Office version: [fill]
  6. Problem with Powerpoint, Excel or Word document: [fill]
docx.generate(out, {
            finalize: function (bytesWritten) {
              console.log(bytesWritten); // it returns 0
              exec(docName); // it can't open the document
            }
        });

the document was successfully generated, but this can't open it

I know docx.generate() function is async method, but how can I open it Doesn't docx.generate.finalize callback function?

Jodge commented 5 years ago

Paste the whole code here so someone can understand, the information above is scanty

Ziv-Barber commented 5 years ago

The right way to do it (and I need to add it to the documentation):

var out = fs.createWriteStream(path.join(outDir, 'example.pptx'))

out.on('error', function(err) {
    console.log(err)
})

out.on('finish', function(err) {
    // This is the real finish in case of creating a document file.
 })

 docx.generate(out)

The finalize event of officegen itself representing that officegen finished to do his tasks but not the external file system write stream.

woosungchu commented 5 years ago

It works !!

out.on('finish', function(err) {
    // This is the real finish in case of creating a document file.
 })

Thank you for reply, Ziv-Barber