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
Add a ment  | 

1 Answer 1

Reset to default 3
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);
});

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
Add a ment  | 

1 Answer 1

Reset to default 3
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);
});

本文标签: javascriptNodejs using officegen to create and download word documentStack Overflow