Using script to manipulate data before passing data to docxtemplater recipe



  • Hi,

    I have been having success using jsreport to produce pdf reports with chrome-pdf and excel tables using html-to-excel.

    In both cases I use handlebars to get the data I am working with and modify it (the data that I use is not suited for a straightforward handlebars template as it needs to be massaged and aggregated a lot). Usually I do this with script tags in the report template html. Eg:

    <script>
    let total = {{{toJSON root}}};
    total.forEach(.....etc ...
    </script>

    This allows me to construct the html that will form the report with both of those recipes.

    I am attempting to use docxtemplater and I am not sure how to get the data that is in my "data" folder in the studio, manipulate it and then send it to the docx template.

    I can make data in the folder, and get the docxtemplater to work as per this example https://jsreport.net/learn/docxtemplater

    But I would like to manipulate the data before it gets to the docx template. My guess is that I need to use a beforeRender function, but I am not sure how to do that.

    Any help is appreciated.



  • My guess is that I need to use a beforeRender function, but I am not sure how to do that.

    Yes exactly, just create a script entity, associate it with the template and put there something like this.

    function beforeRender(req, res) {
      req.data.myProperty = "my modified value"
    }
    

    https://jsreport.net/learn/scripts



  • Great, I got it, thanks Jan


Log in to reply
 

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