Home » Nodejs » How can I get a buffer for a file (image) from CollectionFS

How can I get a buffer for a file (image) from CollectionFS

Posted by: admin November 30, 2017 Leave a comment


I’m trying to insert an image into a pdf I’m creating server-side with PDFkit. I’m using cfs:dropbox to store my files. Before when I was using cvs:filesystem, it was easy to add the images to my pdf’s cause they were right there. Now that they’re stored remotely, I’m not sure how to add them, since PDFkit does not support adding images with just the url. It will, however, accept a buffer. How can I get a buffer from my CollectionFS files?

So far I have something like this:

var portrait = Portraits.findOne('vS2yFy4gxXdjTtz5d');
readStream = portrait.createReadStream('portraits');

I tried getting the buffer two ways so far:

First using dataMan, but the last command never comes back:

var dataMan = new DataMan.ReadStream(readStream, portrait.type());
var buffer = Meteor.wrapAsync(Function.prototype.bind(dataMan.getBuffer, dataMan))();

Second buffering the stream manually:

var buffer = new Buffer(0);
readStream.on('readable', function() {
    buffer = Buffer.concat([buffer, readStream.read()]);
readStream.on('end', function() {

That never seems to come back either. I double-checked my doc to make sure it was there and it has a valid url and the image appears when I put the url in my browser. Am I missing something?


I had to do something similar and since there’s no answer to this question, here is how I do it:

// take a cfs file and return a base64 string
var getBase64Data = function(file, callback) {
  // callback has the form function (err, res) {}
  var readStream = file.createReadStream();
  var buffer = [];
  readStream.on('data', function(chunk) {
  readStream.on('error', function(err) {
    callback(err, null);
  readStream.on('end', function() {
    callback(null, buffer.concat()[0].toString('base64'));

// wrap it to make it sync    
var getBase64DataSync = Meteor.wrapAsync(getBase64Data);

// get a cfs file
var file = Files.findOne();

// get the base64 string
var base64str = getBase64DataSync(file);

// get the buffer from the string
var buffer = new Buffer(base64str, 'base64')

Hope it’ll help!