最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Node.js using officegen to create and download word document - Stack Overflow

matteradmin8PV0评论

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);
});
Post a comment

comment list (0)

  1. No comments so far