繁体   English   中英

html-pdf与ejs模板

[英]html-pdf with ejs template

在我的应用程序中,当用户通过测试时,将自动生成文凭( 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);
    });

这种转换,从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'); 代码与上面的代码,它按我想要的方式工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM