在Node JS中执行代码时,不能通过命令访问文件

我在节点运行时创build一个文件[HTML],并通过命令行将其转换为PDF。 HTML创build成功,甚至PDF文件也创build成功,但空。

我检查了创build的HTML文件包含数据,但命令行无法读取,我猜。 这是我的代码:

创buildHTML文件:

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

创buildHTML后创buildPDF

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

它没有find文件,导致你错过了扩展名

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