Skip to content

Instantly share code, notes, and snippets.

@aregee
Last active November 2, 2015 19:02
Show Gist options
  • Save aregee/01e161bd932b77e22109 to your computer and use it in GitHub Desktop.
Save aregee/01e161bd932b77e22109 to your computer and use it in GitHub Desktop.
var spawn = require('child_process').spawn;
var Stream = require('stream');
var pdfStream = module.exports = function () {
var convertPdfs = function(streamIn){
var command = 'gs';
var args = [
"-dNOPAUSE",
"-sDEVICE=png16m",
"-r600",
"-sOutputFile=-",
"-q",
"-_",
"-c quit"
];
var proc = spawn(command, args);
var stream = new Stream();
proc.stderr.on('data', stream.emit.bind(stream, 'error'));
proc.stdout.on('data', stream.emit.bind(stream, 'data'));
proc.stdout.on('end', stream.emit.bind(stream, 'end'));
proc.on('error', stream.emit.bind(stream, 'error'));
streamIn.pipe(proc.stdin);
return stream;
};
return convertPdfs;
};
app.post('/upload-stream', authFilter, function (req, res) {
var file = req.files['file'];
var fileStream = fs.createReadStream(file.path);
res.writeHead(200, { "Content-Type": "application/json",
"Cache-control": "no-cache" });
var buf = [];
pdfStream(fileStream).on('data', function (chunk) {
buf.push(chunk);
}).on('end', function () {
var splitBy = '�PNG\r\n\u001a\n\u0000\u0000\u0000\rIHDR';
var buffer = Buffer.concat(buf);
console.log(buffer);
var splited = buffer.toString().split(splitBy);
console.log(splited.length);
var imgs = [];
_.forEach(splited, function (k) {
console.log(">>>>>");
if( k !== "") {
imgs.push({'image': new Buffer(k)});
//res.write('<img src=\'data:image/png;base64,' + k.toString('base64') + '\/>');
}
});
res.write(_(JSON.stringify({data: imgs})).toString());
res.end();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment