Send PDF from server to client

I have a project where I generate a PDF on the server, and send it to the client via a meteor method (as a data url). I describe it here Error: EACCES, open · Issue #11 · pascoual/meteor-pdfkit · GitHub, but I’ll copy and paste what I wrote to make things easier:

I return a base64 encoded PDF from a meteor method for my own Meteor application. I started out using this package、but ended up using the npm package pdfkit directly, because I wanted access to the pipe() function. Maybe there is some way to get access to that in this Meteor package?

The advantage to doing this through a meteor method for myself is because I get access to authentication information (this.userId) within the server-side meteor method’s context.

//Code that I wrote as a custom meteor package 
var base64 = Npm.require('base64-stream');
var Future = Npm.require('fibers/future');
var PDFDocument = Npm.require('pdfkit');

somePDFGeneratingFunction(){
var doc = new PDFDocument({…});
//create the pdf with various commands (doc.text, doc.rect, etc)

var future = new Future();
var finalString = “”;
var stream = doc.pipe(base64.encode());
doc.end();
stream.on(‘data’, function(chunk){
finalString += chunk;
});

stream.on(‘end’, Meteor.bindEnvironment(function(){
future.return(finalString);
}));

return future.wait();
}

```javascript
//inside a meteor method
Meteor.methods({
  'reports.generatePDF': function(){
    return somePDFGeneratingFunction();
  }
});
//on the web browser
Meteor.call('reports.generatePDF', function(err, res){
  window.open("data:application/pdf;base64, " + res);
});

It was easier than creating a special route for the client to go to to download the PDF, and plus since it’s a meteor method I get this.userId so I know if they’re logged in or not, and what their access level is.