[英]html-pdf with ejs template
In my application, when a user passes a test, there will automatically be generated a diploma ( pdf
). 在我的应用程序中,当用户通过测试时,将自动生成文凭(
pdf
)。 This I accomplish by using the html-pdf
node-module
, which retrieves the html file and makes it into a pdf. 我通过使用
html-pdf
node-module
来完成这个任务,该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);
});
This conversion, from html
to pdf
, works perfectly and looks really good. 这种转换,从
html
到pdf
,完美运行,看起来非常好。 Now what I want is to make the template
a little bit more dynamic, meaning that depending on the user, their name appears in the pdf
. 现在我想要的是让
template
更具动态性,这意味着根据用户的不同,他们的名字会出现在pdf
。
I have work once with ejs
to generate an email template, so I was thinking that my problem could be solved with that, but as mentioned I'm haven't work that much with ejs
and can't figure out how to send data
into my ejs
file? 我曾经和
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);
}
});
I replaced my var html = fs.readFileSync('./phantomScripts/certificate.html', 'utf8');
我替换了我的
var html = fs.readFileSync('./phantomScripts/certificate.html', 'utf8');
code with the above code, and it works as I want it to. 代码与上面的代码,它按我想要的方式工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.