gpt4 book ai didi

javascript - 带有 ejs 模板的 html-pdf

转载 作者:数据小太阳 更新时间:2023-10-29 06:11:05 27 4
gpt4 key购买 nike

在我的应用程序中,当用户通过测试时,会自动生成一个文凭 (pdf)。这是我通过使用 html-pdf node-module 来完成的,它检索 html 文件并将其转换为 pdf。

var fs = require('fs'),
pdf = require('html-pdf'),
path = require('path'),
ejs = require('ejs');


var html = fs.readFileSync('./phantomScripts/certificate.html', 'utf8');
var options = { filename: 'businesscard.pdf', format: 'A4', orientation: 'portrait', directory: './phantomScripts/',type: "pdf" };

pdf.create(html, options).toFile(function(err, res) {
if (err) return console.log(err);
console.log(res);
});

这种从 htmlpdf 的转换工作完美,看起来非常好。现在我想要的是让 template 更动态一点,这意味着根据用户的不同,他们的名字出现在 pdf 中。

我曾经使用 ejs 生成一个电子邮件模板,所以我认为我的问题可以用它来解决,但正如我提到的那样,我并没有用 ejs 并且不知道如何将 data 发送到我的 ejs 文件中?

最佳答案

ejs.renderFile('./path/to/template-file.ejs', {data : {firstname: 'Tom', lastname: 'Hanks'}, function(err, result) {
// render on success
if (result) {
html = result;
}
// render or error
else {
res.end('An error occurred');
console.log(err);
}
});

我用上面的代码替换了我的 var html = fs.readFileSync('./phantomScripts/certificate.html', 'utf8'); 代码,它按我想要的方式工作。

关于javascript - 带有 ejs 模板的 html-pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30753342/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com