[英]Can't access a file by command while execution of code in Node JS
I'm creating one file [HTML] at run time by Node and converting it to PDF by command line. 我正在运行时通过Node创建一个文件[HTML],然后通过命令行将其转换为PDF。 HTML created successfully even PDF file also created success but empty.
HTML创建成功,即使PDF文件也创建成功,但为空。
I checked the created HTML file contains data but command line couldn't read that I guess. 我检查了创建的HTML文件是否包含数据,但命令行无法读取,我猜是这样。 Here is my code:
这是我的代码:
Create HTML file: 创建HTML文件:
var getPdf=function (html,fileName,callback) {
fs.writeFile("./pdf/"+fileName+".html", html,'utf8', function(err) {
if(err) {
return callback(err,[]);
}
fs.readFile("./pdf/"+fileName+".html","utf8", (err, data) => {
if (err) throw err;
console.log(data);
});
return callback(err,fileName);
});
}
Create PDF after the HTML Created: 在创建HTML之后创建PDF:
var html="<html><head><title>Hello World</title></head><body><h1>Hello
Sarath!</h1></body></html>";
getPdf(html,'sarath',function (err, fileName) {
if(err) {
console.log("Error: "+err);
return false;
}
var cmd="xvfb-run wkhtmltopdf ./pdf/"+fileName+" ./pdf/"+fileName+".pdf";
console.log(cmd);
exec(cmd,function(err,stdout,stderr){
if(err) {
return console.log(err);;
}
console.log("PDF Created!",stdout);
});
});
It coudln't find the file cause you missed the extension 由于找不到扩展名,因此找不到文件
var html="<html><head><title>Hello World</title></head><body><h1>Hello
Sarath!</h1></body></html>";
getPdf(html,'sarath',function (err, fileName) {
if(err) {
console.log("Error: "+err);
return false;
}
var cmd="xvfb-run wkhtmltopdf ./pdf/"+fileName+".html ./pdf/"+fileName+".pdf";
console.log(cmd);
exec(cmd,function(err,stdout,stderr){
if(err) {
return console.log(err);;
}
console.log("PDF Created!",stdout);
});
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.