Take template code from data and render it into the report

  • I use handlebars to generate the the html which is then rendered by chrome to pdf.
    In my application, user can override the default header en footer with custom content.
    The custom html is sent to the jsreport as part of the report data.

    I got this working with static content, but it would be nice if the custom content would be able to interact with other variables in the report data.
    So the custom header or footer content should be injected into the template and interpreted by handlebars.

    Any ideas on how to do this?

  • Using the custom script is probably the simplest. In script, you could prepare the header handlebars content upfront, before execution.

    function beforeRender(req, res) {
      req.template.chrome.headerTemplate += req.data.myCustomHeaderContent

    A more complex option would need you to require handlebars, recompile the template from the user and execute it.

  • Thanks for the quick reply.
    Am I right to assume that beforeRender is executed between handlebars and pdf rendering?
    That would be consistent with the results I see.

  • No, its:

    before render -> templating engine like handlebars -> recipe -> after render

  • Ok, then I have some debugging to to do ;-)

  • Ok, this worked out very well for me.
    I had some quirk, with css being applied differently between my hard coded footer and the dynamically assembled footer, but I got around that.
    Just not sure what happened

    Thanks for the support

Log in to reply

Looks like your connection to jsreport forum was lost, please wait while we try to reconnect.