2

I am developing a system where the User need to download a PDF file that was generated by phantomJS and saved on the server. This is the code that generates the file and saved to the server:

var pdf = Meteor.require('phantomjs-pdf');
    var options = {'html': data};

    pdf.convert(options, function(result) {

        /* Using a buffer and callback */
        result.toBuffer(function(returnedBuffer) {});

        /* Using a readable stream */
        var stream = result.toStream();

        /* Using the temp file path */
        var tmpPath = result.getTmpPath();

        /* Using the file writer and callback */
        result.toFile("/tmp/file.pdf", function() {});
    });

It appears that at phantomJS can not download then how can I download the file?

Murilo Venturoso
  • 359
  • 5
  • 16
  • I'm not sure if its the same problem, but you could try the answer to [this question](http://stackoverflow.com/questions/21565991/how-to-serve-a-file-using-iron-router-or-meteor-itself). – David Weldon Jul 14 '14 at 18:53

1 Answers1

2

General solution (source code)

if (Meteor.isClient) {
  Template.view.events({
    'click button': function () {
      // generate file on server side
      Meteor.call('generateFile', function (error, result) {
          if(error){
              console.error("generateFile error: " ,error);
              return;
          }
          if(result && result.url){
              console.log("File was generated. Trying to download.")
              window.open(result.url);
          }else{
              console.error("Incorrect data returned from 'generateFile' method");
          }
      });
    }
  });
}

if (Meteor.isServer) {
    Future = Npm.require('fibers/future');
    Meteor.methods({
      'generateFile': function (){
           var fut = new Future();
           // async file generation simulated by setTimeout
           setTimeout(function(){
               // here server side should generate pdf, save it to disk and
               // return url to file
               fut["return"]({url:"http://google.com"});
           },2000)
           return fut.wait();
       }
   });
}
Kuba Wyrobek
  • 5,273
  • 1
  • 24
  • 26
  • some browser will block the window.open popup because the Meteor.call is asynchronous and google.com is cross domain. if generated pdf url is on same domain it should work – fadomire May 16 '18 at 13:44