node express how to render handlebars html page to file

前端 未结 3 959
陌清茗
陌清茗 2020-12-16 00:48

I want to convert some html page to pdf via wkhtmltopdf. However, the html page I want to convert to pdf is dynamically generated using handlebars.

So I think one s

3条回答
  •  醉梦人生
    2020-12-16 01:31

    Using express-handlebars, you should use the advanced mode and create an instance of it like in this example.

    The proper way would be to create a view file (like you probably already have per you question) and use the express handlebars instance to render it:

    // init code
    var exphbs = require('express-handlebars');
    var hbs = exphbs.create({
        defaultLayout: 'your-layout-name',
        helpers: require("path-to-your-helpers-if-any"),
    });
    app.engine('.file-extention-you-use', hbs.engine);
    app.set('view engine', '.file-extention-you-use');
    
    // ...then, in the router
    hbs.render('full-path-to-view',conext, options).then(function(hbsTemplate){
         // hbsTemplate contains the rendered html, do something with it...
    });
    

    HTH

提交回复
热议问题