【发布时间】:2022-01-03 03:09:56
【问题描述】:
我正在将我的 handelbrake 文件编译为模板,然后使用 puppeteer 生成 pdf,但 hbs.complie() 函数返回未定义。
这里是渲染模板的函数
async function renderTemplate(data, templateName) {
const filePath = path.join(__dirname, "templates", `${templateName}.hbs`);
if (!filePath) {
throw new Error(`Could not find ${templateName}.hbs in generatePDF`);
}
console.log(filePath);
const html = await fs.readFile(filePath, "utf-8");
return hbs.compile(html)(data);
}
我在 express 中这样使用这个函数:
app.get("/generate-pdf", async (req, res) => {
const htmlContent = await renderTemplate({ name: "test" }, "test");
console.log("Content: ", htmlContent);
await generatePDF("test.pdf", htmlContent);
res.sendFile(path.join(__dirname, "test.pdf"));
});
我似乎不知道问题是什么,有人可以帮忙。
【问题讨论】:
标签: node.js express handlebars.js