Benjamin Byholm
9 years ago
5 changed files with 183 additions and 0 deletions
@ -0,0 +1,59 @@ |
|||
'use strict'; |
|||
|
|||
/*! |
|||
* Canvas - PDFStream |
|||
*/ |
|||
|
|||
/** |
|||
* Module dependencies. |
|||
*/ |
|||
|
|||
var Stream = require('stream').Stream; |
|||
|
|||
/** |
|||
* Initialize a `PDFStream` with the given `canvas`. |
|||
* |
|||
* "data" events are emitted with `Buffer` chunks, once complete the |
|||
* "end" event is emitted. The following example will stream to a file |
|||
* named "./my.pdf". |
|||
* |
|||
* var out = fs.createWriteStream(__dirname + '/my.pdf') |
|||
* , stream = canvas.createPDFStream(); |
|||
* |
|||
* stream.pipe(out); |
|||
* |
|||
* @param {Canvas} canvas |
|||
* @param {Boolean} sync |
|||
* @api public |
|||
*/ |
|||
|
|||
var PDFStream = module.exports = function PDFStream(canvas, sync) { |
|||
var self = this |
|||
, method = sync |
|||
? 'streamPDFSync' |
|||
: 'streamPDF'; |
|||
this.sync = sync; |
|||
this.canvas = canvas; |
|||
this.readable = true; |
|||
// TODO: implement async
|
|||
if ('streamPDF' == method) method = 'streamPDFSync'; |
|||
process.nextTick(function(){ |
|||
canvas[method](function(err, chunk, len){ |
|||
if (err) { |
|||
self.emit('error', err); |
|||
self.readable = false; |
|||
} else if (len) { |
|||
self.emit('data', chunk, len); |
|||
} else { |
|||
self.emit('end'); |
|||
self.readable = false; |
|||
} |
|||
}); |
|||
}); |
|||
}; |
|||
|
|||
/** |
|||
* Inherit from `EventEmitter`. |
|||
*/ |
|||
|
|||
PDFStream.prototype.__proto__ = Stream.prototype; |
Loading…
Reference in new issue