admin管理员组文章数量:1026989
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?
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?
Share Improve this question asked Feb 15, 2017 at 7:40 codeinprogresscodeinprogress 3,5018 gold badges53 silver badges76 bronze badges 2-
1
If you're using latest officegen, then out should be the file stream, not just file. Refer to manual github./Ziv-Barber/officegen . Also you can generate directly to response like
docx.generate(res);
– Andrey Commented Feb 15, 2017 at 8:40 - @Andrey thanks. I did the doc.generate(res) and it worked. Appreciate your help – codeinprogress Commented Feb 15, 2017 at 11:33
1 Answer
Reset to default 3router.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);
});
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?
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?
Share Improve this question asked Feb 15, 2017 at 7:40 codeinprogresscodeinprogress 3,5018 gold badges53 silver badges76 bronze badges 2-
1
If you're using latest officegen, then out should be the file stream, not just file. Refer to manual github./Ziv-Barber/officegen . Also you can generate directly to response like
docx.generate(res);
– Andrey Commented Feb 15, 2017 at 8:40 - @Andrey thanks. I did the doc.generate(res) and it worked. Appreciate your help – codeinprogress Commented Feb 15, 2017 at 11:33
1 Answer
Reset to default 3router.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);
});
本文标签: javascriptNodejs using officegen to create and download word documentStack Overflow
版权声明:本文标题:javascript - Node.js using officegen to create and download word document - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745658311a2161729.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论