您好,我正在编写一个nodejs方法,希望将ejs文件转换为pdf文件
router.get("/uknow", (req, res) => {
ejs.renderFile(path.join(__dirname, "../../views/resultat.ejs"), (err, data) => {
if (err) {
res.send(err);
} else {
pdf.create(data).toFile("report.pdf", function (err, data) {
if (err) {
res.send(err);
} else {
res.send("File created successfully");
}
});
}
});
})
这是我编写的方法,我在邮递员中得到了此回复
{
"path": "C:\\Users\\HP\\Desktop\\exemple CRUD\\blog_api\\views\\resultat.ejs"
}
我打印出错误,这就是我得到的ReferenceError:
C:\Users\HP\Desktop\exemple CRUD\blog_api\views\resultat.ejs:41
39| </head>
40| <body class="spacer">
>> 41| <h2 class="text-center my-3"><%= data[0].imei.name %></h2>
42| <h5 class="text-center my-3">Imei -<%= data[0].imei.name %> </h5>
43| <h3 class="text-center my-3">Resultat des tests</h3>
44| <hr>
data is not defined
at eval (eval at compile (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\ejs\lib\ejs.js:661:12), <anonymous>:10:26)
at resultat (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\ejs\lib\ejs.js:691:17)
at tryHandleCache (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\ejs\lib\ejs.js:272:36)
at Object.exports.renderFile (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\ejs\lib\ejs.js:489:10)
at C:\Users\HP\Desktop\exemple CRUD\blog_api\routes\apis\post.js:121:9
at Layer.handle [as handle_request] (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\layer.js:95:5)
at next (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\route.js:137:13)
at Route.dispatch (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\route.js:112:3)
at Layer.handle [as handle_request] (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\layer.js:95:5)
at C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:281:22
at Function.process_params (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:275:10)
at Function.handle (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:174:3)
at router (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:47:12)
at Layer.handle [as handle_request] (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\HP\Desktop\exemple CRUD\blog_api\node_modules\express\lib\router\index.js:317:13) {
path: 'C:\\Users\\HP\\Desktop\\exemple CRUD\\blog_api\\views\\resultat.ejs'
}
并且在控制台中什么也不需要,我需要一些帮助,谢谢您
首先呈现文件(连同您的数据),然后将呈现的HTML传递给pdf'er
未经测试,请尝试以下方法:
router.get("/uknow", (req, res) => {
// your missing data, wheres it coming from?
// it should also look like:
// let data = { data: [{ imei: { name: '', ...}}, ...], ...}
// render the ejs file
ejs.renderFile(path.join(__dirname, '..', '..', 'views', 'resultat.ejs'), data, {}, function(err, str) {
if (err) return res.send(err);
// str now contains your rendered html
pdf.create(str).toFile("report.pdf", function(err, data) {
if (err) return res.send(err);
res.send("File created successfully");
});
});
});