Using JReport to export a PDF report in CSV format



  • This is the use case i am trying to achieve in JSReport

    1. There is a webapp that renders a PDF report in a view
    2. An "Export" option is provided to user to export the report as CSV
    3. In JSReport, is there any rest end point or a method call using which I can export
    4. I am using scripts to make data call to an external API
    5. I know that I can call /api/report with ""recipe": "text"" and get a CSV report
    6. If I again make this rest call when user clicks on export button, then there will be total 4 server calls from the time user made a request to generate PDF report
      6.1) First call to JSReport server by /api/report end point
      6.2) Second call by the scripts to get report data from external API
      6.3) PDF report is generated and user wants to export in CSV format
      6.4) Third call again to JSReport by /api/report end point, this time for CSV report format
      6.5) Fourth call by the scripts to get report data from external API to generate CSV report
      Is there any other way this can be achieved without having to make additional trips to JSReport to export data in CSV format?

    Thanks in advance



  • You can try it this way...

    Attach to the pdf template this custom script:

    const jsreport = require('jsreport-proxy')
    
    async function afterRender(req, res) {     
        const csv = await jsreport.render({
            template: {
                name: 'csv'
            },
            data: req.data
        })
    
        res.content = Buffer.concat([res.content, Buffer.from('$$$'), csv.content])
    }
    

    It will render the csv template and append the result to the output stream separated using $$$ string.
    You can split this on your server and have two buffers. One with pdf and one with csv.


Log in to reply
 

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