Node express how to display hbml hbml page in file

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 rudders.

So, I think that one solution can be connected with creating an html page using descriptors, but with a file (html file). Then convert this file to pdf using hkhtmltopdf, then allow the user to somehow download the PDF file.

So my question is: how can I process a dynamically generated html (handlebars) page into a file?

Thanks and goodbye...

+4
source share
3 answers

A simple example for creating a file.

var Handlebars = require('handlebars');

var source = "<p>Hello, my name is {{name}}. I am from {{hometown}}. I have " +
    "{{kids.length}} kids:</p>" +
    "<ul>{{#kids}}<li>{{name}} is {{age}}</li>{{/kids}}</ul>";
var template = Handlebars.compile(source);

var data = { "name": "Alan", "hometown": "Somewhere, TX",
    "kids": [{"name": "Jimmy", "age": "12"}, {"name": "Sally", "age": "4"}]};
var result = template(data);


var fs = require('fs');
    fs.writeFile("test.html", result, function(err) {
    if(err) {
        return console.log(err);
    }
});
+8
source

express-handlebars, , .

- (, , , ), expressbars :

// 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...
});

+3

Alex . , "express-handlebars", "handlebars". , Express-Handlebars - Handlebars Express, . compile() Express-Handlebars, Handlebars () (html) , , .

In short: 1) I know that Express-Handlebars are Handlebars for Express. 2) I do not know how to use the compile () method only from express-handlebars, so I finished installing Handlebars (from npm) and used it on the server to create my html file (from the template) and saved it to disk. 3) Of course, I installed and used Express-Handlebars all over the world to serve my pages in my Express application; just install Handlebars to create my html (on the server) using the "compile ()" method and save the result to disk.

I hope this is clear. Thanks again...

0
source

All Articles