带有 EJS 模板的 HTML-PDF
html-pdf with ejs template
在我的应用程序中,当用户通过测试时,将自动生成一个文凭(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
文件中?
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或html下载PDF格式的填写表单
- 在提交时打开thankyou.html+下载PDF
- 生成pdf或其他非html文件时的错误处理
- how to convert html <div> to pdf
- 如何在使用jsPDF将html转换为pdf的同时,在pdf中的某个点进行分页
- 如何将带有大量svg图表的html转换为pdf
- 将php页面的一个部分渲染为HTML和CSS后生成PDF,并考虑PDF分页符
- 如何使用javascript或jquery将整个html页面转换为pdf
- 如果iframe在src(pdf或text)中有非html文档,则iframe中的onload不起作用
- HTML表格导出为具有适当列宽和字体的pdf
- 具有可拖动元素的HTML到PDF
- c# mvc nreco HtmlToPdfConverter 问题将 html 文档转换为 pdf
- 来自HTML图像的jsPDF 会导致 pdf 为空
- 使用 php 将 pdf 文件转换为 html 文件
- HTML到PDF的转换仅在客户端使用JavaScript
- 使用EJS模板进行HTML验证;文本/模板”;元素
- 在html中嵌入文档(html/pdf/swf/…)的统一方法
- 使用html-pdf处理动态数据
- 使用node-html-pdf模块下载生成的PDF
- 带有 EJS 模板的 HTML-PDF