html-docs.js生成的doc是空白的

时间:2018-05-23 13:14:17

标签: node.js express npm

我是节点js的新手。我正在学习快递js。我想从html内容创建word文件。我正在使用 html-docs-js 。 Word在文件夹中生成。但是这个词没有任何内容。 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");
        }
    });
  });

这可能对某人有用..

1 个答案:

答案 0 :(得分:0)

fs.readFile(&#39; ./ index.ejs&#39;,&#39; utf-8&#39;,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");
    }
});

});

相关问题