Search code examples
javascriptnode.jsjsreport

Rendering html-to-xlsx recipe response from JSReport into a downloadable excel sheet


I have patient data in my databases represented in HTML that I want to print as an excel sheet using JSReport. But when I get the response from the jsreport server with the recipe it reads jibberish and I can't find a way to render it into an excel file.

Dummy example

const axios = require("axios");

const url = "http://localhost:5488/api/report/";

const data = {
  "template": {
      "shortid": "2_Kw0BRuOm",
      "recipe": "html-to-xlsx",
      "data": {
          "people": [
              {
                  "name": "Omar rafat",
                  "age": "20",
                  "job": "Software Engineer"
              }
          ]
      }
  }
}

axios.post(url, {
  "template": {
      "shortid": "2_Kw0BRuOm",
      "recipe": "html-to-xlsx",
      "data": {
          "people": [
              {
                  "name": "Omar rafat",
                  "age": "20",
                  "job": "Software Engineer"
              }
          ]
      },
      "options": { "reports": { "save": true } }
  }
}).then(res => console.log(res.data)).catch(err => console.log(err));

Raw html-to-xlsx response

enter image description here

I might have missed up on the library's usage due to my misunderstanding of how the technology works but I really hope some of you might guide me on the correct implementation of it.

Thanks.


Solution

  • jsreport has nodejs client you can use https://jsreport.net/learn/nodejs-client

    You can check the following example on how to render a template and store the output to the file.

    const client = require('jsreport-client')('http://localhost:5488')
    const fs = require('fs').promises
    
    
    async function render () {
      const res = await client.render({
        template: { shortid: '2_Kw0BRuOm' },
        data: { someText: 'world!!' }
      })
    
      const responseBuffer = await res.body()
      await fs.writeFile('out.xlsx', responseBuffer)
    }
    
    render().catch(console.error)