First, make sure to require the officegen module:
var officegen = require('officegen')
There are two ways to use the officegen returned function to create an officegen stream:
var myDoc = officegen('<type of document to create>')
// or:
var myDoc = officegen({
'type': '<type of document to create>'
// More options here (if needed)
})
// Supported types:
// 'pptx' or 'ppsx' - Microsoft PowerPoint based document.
// 'docx' - Microsoft Word based document.
// 'xlsx' - Microsoft Excel based document.
Generating an empty Microsoft PowerPoint officegen stream:
var pptx = officegen('pptx')
Generating an empty Microsoft Word officegen stream:
var docx = officegen('docx')
Generating an empty Microsoft Excel officegen stream:
var xlsx = officegen('xlsx')
General events of the officegen stream:
Event examples:
pptx.on('finalize', function (written) {
console.log('Finish to create a PowerPoint file.\nTotal bytes created: ' + written + '\n')
})
pptx.on('error', function (err) {
console.log(err)
})
Another way to register either 'finalize' or 'error' events:
var pptx = officegen({
'type': 'pptx', // or 'xlsx', etc
'onend': function (written) {
console.log('Finish to create a PowerPoint file.\nTotal bytes created: ' + written + '\n')
},
'onerr': function (err) {
console.log(err)
}
})
If you are preferring to use callbacks instead of events you can pass your callbacks to the generate method
(see below).
Now you should fill the object with data (we'll see below) and then you should call generate with
an output stream to create the output Office document.
Example with pptx:
var out = fs.createWriteStream('out.pptx')
pptx.generate(out)
out.on('close', function () {
console.log('Finished to create the PPTX file!')
})
Passing callbacks to generate:
var out = fs.createWriteStream('out.pptx')
pptx.generate(out, {
'finalize': function (written) {
console.log('Finish to create a PowerPoint file.\nTotal bytes created: ' + written + '\n')
},
'error': function (err) {
console.log(err)
}
})
Generating HTTP stream example (no file been created):
var http = require('http')
var officegen = require('officegen')
http.createServer(function (request, response) {
response.writeHead (200, {
'Content-Type': 'application/vnd.openxmlformats-officedocument.presentationml.presentation',
'Content-disposition': 'attachment; filename=surprise.pptx'
})
var pptx = officegen('pptx')
pptx.on('finalize', function (written) {
// We don't really need it in this case.
})
pptx.on('error', function (err) {
// Error handing...
})
// ... (fill pptx with data)
// Generate the Powerpoint document and sent it to the client via http:
pptx.generate(response)
}).listen(3000)
The default Author of all the documents been created by officegen is 'officegen'. If you want to put anything else please
use the 'creator' option when calling the officegen function:
var pptx = officegen({
'type': 'pptx', // or 'xlsx', etc.
'creator': '<your project name here>'
})
Change the document title (pptx,ppsx,docx):
var pptx = officegen({
'type': 'pptx',
'title': '<title>'
});
// or
pptx.setDocTitle('<title>')
For Word only:
var docx = officegen({
'type': 'docx',
'subject': '...',
'keywords': '...',
'description': '...'
});
// or
docx.setDocSubject('...')
docx.setDocKeywords('...')
docx.setDescription('...')
If needed, you can activate some verbose messages (warning: this does not cover all part of the lib yet) with :
officegen.setVerboseMode(true)