Generate pdf and excel on nodejs server call



  • Hi,
    How can generate a pdf or excel using shortid
    I have tried this code but doesn't work.

    static async BIReporting(req:Request, res:Response){
    console.log('Quering JS Reports');

    try {
    
        const data = {
          template: { shortid: 'RHdf7jP' },
          data: {test: 'test'},
          options: {'Content-Disposition': `attachment; filename=test.pdf`}
        };
    
        const options = {
          method: 'POST',
          headers: {
            'Authorization': 'Basic ' + base64("admin:password"),
            'Content-Type': 'application/json',
          },
          uri: 'https://villageco.jsreportonline.net/api/report',
          json: data
        };
    
        request(options).pipe(res);
      } catch(err) {
        console.log(`Error creating pdf ${err} `)
        return res.sendStatus(500)
      }
    
    // async function render () {
    //     const res = await client.render({
    //       template: {
    //           'shortid':'RHdf7jP',
    //       },
    //       data: { someText: 'world!!' }
    //     })
      
    //     const bodyBuffer = await res.body()
    //     console.log(bodyBuffer.toString())
    //   }
      
    //   render().catch(console.error)
    
    
    }

  • administrators

    what is the error message that you get?

    one thing to highlight when using jsreportonline is that the user that you pass must have the permissions enabled, if you are using your admin account then it should work fine, but if you are using one user that you created then you should check if that user has permissions to read the template that you are trying to render


  • administrators

    also FYI i think request package has an option called auth that you can pass { username: 'user here', password: 'password here' } , so no need to set the header manually.



  • I don't get any error
    these are the logs
    +0 Starting rendering request 572743 (user: insixxxoxx@gmail.com)
    +4 Rendering template { name: Invoice, recipe: chrome-pdf, engine: handlebars, preview: true }
    +6 Adding sample data m_mZYKg
    +7 Resources not defined for this template.
    +13 Replaced assets ["Invoice styles.css"]
    +13 Rendering engine handlebars
    +31 Taking compiled template from engine cache
    +32 Calculating item Website design; you should see this message in debug run
    +32 Calculating item Website implementation; you should see this message in debug run
    +48 Executing recipe chrome-pdf
    +669 Running chrome with params {"printBackground":true,"landscape":false,"margin":{}}
    +1116 Skipping storing report.



  • With this code I'm able to save it to a disk but how to trigger a download with same code
    const client = require('jsreport-client')('https://bjrmatos9.jsreportonline.net/', 'admin', 'password')

    client.render({
    ...your options here...
    }).then((response) => {
    // response is a stream, you can use it and save it to disk
    response.pipe(fs.createWriteStream('/some/path/to/store.pdf'))
    }).catch((err) => console.error(err))


  • administrators

    you can try the following code and it should give you the pdf as download in browser, of course you need to execute this on your webserver, with express it looks like this

    app.get('/report', async (req, res) => {
      try {
        const resp = await client.render({
          template: {
            content: '<h1>sample</h1>',
            engine: 'handlebars',
            recipe: 'chrome-pdf'
          }
        })
    
        const reportName = 'report.pdf'
    
        // set response headers to trigger the browser download
        res.set({
          'Content-Type': resp.headers['content-type'],
          'Content-Disposition': `attachment; filename=${reportName}`,
        })
    
        resp.pipe(res)
      } catch (e) {
        res.status(500).send(e.message)
      }
    })
    

Log in to reply
 

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