在我的应用程序中,当用户通过测试时,将自动生成文凭(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);
});这个从html到pdf的转换工作得很完美,看起来也很不错。现在我想要的是使template更具动态性,这意味着根据用户的不同,他们的名字会出现在pdf中。
我曾经使用ejs生成一个电子邮件模板,所以我想我的问题可以用它来解决,但如上所述,我还没有使用ejs那么多,并且不知道如何将data发送到我的ejs文件中?
发布于 2015-06-10 20:59:31
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');代码,它就像我想要的那样工作。
https://stackoverflow.com/questions/30753342
复制相似问题