How to download dynamically generated pdf?


#1

I’m generating a PDF file on the server. File is fine on the server as I can open it directly from the file system. I’m using meterohacks Picker to send it to the client. When it gets downloaded its slightly smaller (3.4 megs instead of 3.5). Nothing appears in the downloaded pdf (empty). I know folks use CollectionFS for this stuff, but I’d really like to be able to do this ‘by hand’. I’ve tried different encoding, different headers… either error or same ‘empty’ file.

Picker.route('/download/:filename', function(params, req, res, next) {
    console.log('/download route!');
    var fs = Npm.require('fs'); 
    var path = Npm.require('path'); 
    var theDir = path.resolve('./tmp');
    console.log('the down load directory is: ', theDir);
    var filename = params.filename;
    var fileContent = fs.readFileSync(theDir + '/' + filename, {
        encoding: 'utf8'
    });
    res.setHeader('Content-type', 'application/pdf');
    res.end(fileContent);
});

Any help on what I’m missing would be great!
P:)