node.js - html-docs.js 生成的文档是空白的
问题描述
我是节点 js 的新手。我正在学习 express 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");
}
});
});
这可能对某人有用..
解决方案
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");
}
});
});
推荐阅读
- javascript - 将第三方 javascript 小部件动态添加到 Web 应用程序
- javascript - 使用 post 数据获取 expressjs 导致找不到 404 服务器
- javascript - 滚动顶部动画在 IOS 上不起作用
- ios - 'LGsideMenucontroller' 未在 swift 4 的所有视图控制器中打开
- java - 如何修复“删除令牌连接超时后重新连接 SunPKCS11”
- android - 'java.lang.String' 在 Android 中已弃用
- php - 如何使用 PHP 在会话中传递单选按钮的值?
- node.js - 在 Elasticsearch JS 中使用复合查询面临困难
- javascript - Bootstrap 折叠滚动到底部问题
- android - 除非重叠文本视图,否则是否可以在约束布局中将图标居中