Reputation: 3501
I want to use officegen npm module to create a word (docx) file and download it. In the past I have used tempfile module to create a temporary path for downloading purpose. Here is the code I have written for word doc download:
var tempfile = require('tempfile');
var officegen = require('officegen');
var docx = officegen('docx');
router.post('/docx', function(req, res){
var tempFilePath = tempfile('.docx');
docx.setDocSubject ( 'testDoc Subject' );
docx.setDocKeywords ( 'keywords' );
docx.setDescription ( 'test description' );
var pObj = docx.createP({align: 'center'});
pObj.addText('Policy Data', {bold: true, underline: true});
docx.on('finalize', function(written) {
console.log('Finish to create Word file.\nTotal bytes created: ' + written + '\n');
});
docx.on('error', function(err) {
console.log(err);
});
docx.generate(tempFilePath).then(function() {
res.sendFile(tempFilePath, function(err){
if(err) {
console.log('---------- error downloading file: ' + err);
}
});
});
});
However it gives me an error saying
TypeError: dest.on is not a function
The total bytes created are also shown 0. What am I doing wrong?
Upvotes: 1
Views: 7771
Reputation: 3501
router.post('/docx', function(req, res){
var tempFilePath = tempfile('.docx');
docx.setDocSubject ( 'testDoc Subject' );
docx.setDocKeywords ( 'keywords' );
docx.setDescription ( 'test description' );
var pObj = docx.createP({align: 'center'});
pObj.addText('Policy Data', {bold: true, underline: true});
docx.on('finalize', function(written) {
console.log('Finish to create Word file.\nTotal bytes created: ' + written + '\n');
});
docx.on('error', function(err) {
console.log(err);
});
res.writeHead ( 200, {
"Content-Type": "application/vnd.openxmlformats-officedocument.documentml.document",
'Content-disposition': 'attachment; filename=testdoc.docx'
});
docx.generate(res);
});
Upvotes: 3