gpt4 book ai didi

javascript - Node.js 使用 officegen 创建和下载 word 文档

转载 作者:行者123 更新时间:2023-11-30 11:42:07 25 4
gpt4 key购买 nike

我想使用 officegen npm 模块创建一个 word (docx) 文件并下载它。过去我使用 tempfile 模块创建一个临时路径用于下载目的。这是我为 word 文档下载编写的代码:

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);
}
});
});
});

但是它给了我一个错误提示

TypeError: dest.on is not a function

创建的总字节数也显示为 0。我做错了什么?

最佳答案

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);
});

关于javascript - Node.js 使用 officegen 创建和下载 word 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42243300/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com