How to generate pdf report from dynamic data using Phantomjs and webshot?


#1

i am trying to generate a report in pdf format using phantomjs and webshot packages but the problem is i am unable to access a single record from multiple collections with the userId from server to generate my report am using this code…
‘reports/generate_pdf’: function() {

    if (Meteor.isServer) {

  // SETUP
  // Grab required packages
  var webshot = require('webshot');
  var fs      = Npm.require('fs');
  var Future = Npm.require('fibers/future');

  var fut = new Future();

  var fileName = "data.pdf";

  // GENERATE HTML STRING
  var css = Assets.getText('style.css');

  SSR.compileTemplate('layout', Assets.getText('layout.html'));

  Template.layout.helpers({
    getDocType: function() {
      return "<!DOCTYPE html>";
    }
  });

  SSR.compileTemplate('reports', Assets.getText('reports.html'));

  // PREPARE DATA
    var patient = Patients.find({parentId:Meteor.userId()});
    var data = {
    patient: patient       
    }

  var html_string = SSR.render('layout', {
    css: css,
    template: "reports",
    data: data
  });

  // Setup Webshot options
  var options = {
      "paperSize": {
          "format": "Letter",
          "orientation": "portrait",
          "margin": "1cm"
      },
      siteType: 'html'
  };

  // Commence Webshot
  console.log("Commencing webshot...");
  webshot(html_string, fileName, options, function(err) {
      fs.readFile(fileName, function (err, data) {
          if (err) {
              return console.log(err);
          }

          fs.unlinkSync(fileName);
          fut.return(data);

      });
  });
  
  let pdfData = fut.wait();
  let base64String = new Buffer(pdfData).toString('base64');

  return base64String;
}

}

});

Can anyone help me out of this please.


#2

You could try using this.userId instead of Meteor.userId()? If that doesn’t work try using a find with no parameters (i.e. Patients.find() ) and see if it returns anything. Otherwise your code seems fine.