node.js - html-docs.js 生成的文档为空

标签 node.js express npm

我是 Node.js 新手。我正在学习express js。我想从 html 内容创建 word 文件。我正在使用 html-docs-js。 Word 在文件夹中生成。但这个词没有任何内容。单词为空。

我的代码

router.get('/:id', function(req, res){
 var HtmlDocx = require('html-docx-js');
 var fs = require('fs');
 var html = '<p>My content<p>';

 var docx = HtmlDocx.asBlob(html);
 fs.writeFile('myword.docx',docx, function (err){
    if (err) return console.log(err);
    console.log('done');
 });

 res.send('File Generated in Folder');

});

使用代码后

router.get('/:id', function(req, res){
var HtmlDocx = require('html-docx-js');
var fs = require('fs');
var html = '<p>My content<p>';

var docx = HtmlDocx.asBlob(html);
fs.writeFile('myword.docx',docx, function (err){
    console.log('first');
if (err) return console.log(err);
console.log('done');
res.send('File Generated in Folder');
console.log('last');
});
});

在控制台中正确打印。但文件仍然是空白。

最后我在使用以下代码时得到了解决方案。它对我来说效果很好。

  fs.readFile('./index.ejs', 'utf-8', function(err, html) {
    if (err) throw err;

    // var docx = htmlDocx.asBlob(html);
    fs.writeFile('newgenword.txt', html, function(err) {
        if (err) throw err;
        else{
            console.log("Docx has been created");
        }
    });
  });

这可能对某人有用..

最佳答案

fs.readFile('./index.ejs', 'utf-8', function(err, html) { if (err) 抛出错误;

// var docx = htmlDocx.asBlob(html);
fs.writeFile('newgenword.txt', html, function(err) {
    if (err) throw err;
    else{
        console.log("Docx has been created");
    }
});

});

关于node.js - html-docs.js 生成的文档为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50489409/

相关文章:

node.js - npm 插件 "onchange"无法识别 scss 文件更改

node.js - 您如何在 Node Express 应用程序周围传递对象?

node.js - res.cookie(名称,值);不设置 cookie (Express/NodeJs)

node.js - ubuntu VM 上的 node-expat 安装错误

javascript - Mongoose Delete 和 Express app.delete 有什么区别

node.js - Pg-promise:返回查询结果的正确方法

javascript - 这个 npm semver 有什么问题?

node.js - Multer 图像上传错误 : Unexpected token - in JSON at position 0, SyntaxError: JSON 中位置 0 的意外标记 #

node.js - 库 - TypeError : Cannot read property 'zcard' of null

javascript - 为什么我的超时重试得到未定义的结果