node.jspdfjsreportrequestjs

JSReport API. Save PDF from Response


Using JSReport to generate some output for print, and I'm having some trouble saving the PDF file that is returned from the API.

I'm using this code to save the file:

var options = { method: 'POST',
        url: 'http://192.168.100.64:5488/api/report',
        headers:
         { 'postman-token': '81e5ced9-d7b1-80bd-5948-a6934e67d4ae',
           'cache-control': 'no-cache',
           'content-type': 'application/json' },
        body:
         { template: { shortid: 'HJsjiZhob' },
           data:
            { Badges: badges },
           options: { 'Content-Disposition': 'Attachment; filename=badges.pdf' } },
        json: true };

      rq(options, function (error, response, body) {
        if (error) throw new Error(error);
        console.dir(body);
        // fs.writeFileSync(path.join(config.outFolder, 'badges.pdf'), body, 'binary');
        // console.log('Wrote PDF File');
        fs.writeFile(path.join(config.outFolder, 'badges.pdf'), body, 'binary', (err) => {
          if(err) log.error(err);
          log.info('Successfully Wrote Badge Sheet.');
        });
      });

but the PDF is blank, but I can confirm with PostMan that the report works with the following Code:

var options = { method: 'POST',
  url: 'http://192.168.100.64:5488/api/report',
  headers: 
   { 'postman-token': '81e5ced9-d7b1-80bd-5948-a6934e67d4ae',
     'cache-control': 'no-cache',
     'content-type': 'application/json' },
  body: 
   { template: { shortid: 'HJsjiZhob' },
     data: 
      { Badges: 
         [ { Event: 'Event Name',
             Email: 'someone@somewhere.com',
             Attended: '',
             'First Timer': '',
             'Last Name': '---',
             Name: 'Jim',
             Address: 'AddressLine',
             'City, State Zipcode': 'Charleston, WV 25311',
             City: 'Charleston,',
             State: 'WV',
             zipcode: '25311' } ] },
     options: { 'Content-Disposition': 'Attachment; filename=badges.pdf' } },
  json: true };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
var options = { method: 'POST',
  url: 'http://192.168.100.64:5488/api/report',
  headers: 
   { 'postman-token': '81e5ced9-d7b1-80bd-5948-a6934e67d4ae',
     'cache-control': 'no-cache',
     'content-type': 'application/json' },
  body: 
   { template: { shortid: 'HJsjiZhob' },
     data: 
      { Badges: 
         [ { Event: '2017 West Central Regional Forum',
             Email: 'Jimwithrow@wvbaldy.com',
             Attended: '',
             'First Timer': '',
             'Last Name': 'Withrow',
             Name: 'Jim',
             Address: '1578 Kanawha Blvd., Apt. # E 8C',
             'City, State Zipcode': 'Charleston, WV 25311',
             City: 'Charleston,',
             State: 'WV',
             zipcode: '25311' } ] },
     options: { 'Content-Disposition': 'Attachment; filename=badges.pdf' } },
  json: true };

request(options, function (error, response, body) {
  if (error) throw new Error(error);

  console.log(body);
});

The first block of code saves the file as a blank PDF with a number of pages, the second block when used with postman generates a file save dialog with the appropriate text on the page for printing.

Where is the bug?


Solution

  • According to JSReport's admin team, the proper way to use this in Nodejs and Request is as follows:

      var options = { method: 'POST',
        url: 'http://192.168.100.64:5488/api/report',
        headers:
         { 'postman-token': '81e5ced9-d7b1-80bd-5948-a6934e67d4ae',
           'cache-control': 'no-cache',
           'content-type': 'application/json' },
        body:
         { template: { shortid: 'HJsjiZhob' },
           data:
            { Badges: badges },
           options: { 'Content-Disposition': 'Attachment; filename=badges.pdf' } },
        json: true };
        rq(options)
          .on('response', (response) => {
            console.log(response.statusCode);
            console.log(response.headers['content-type']);
          })
          .on('error', (err) => {throw new Errror(err)})
          .on('end', () => log.info('Successfully Wrote Badge Sheet'))
          .pipe(fs.createWriteStream(path.join(config.outFolder, 'badges.pdf')));