canvas
Version:
Canvas graphics API backed by Cairo
59 lines (48 loc) • 1.28 kB
JavaScript
;
/*!
* Canvas - PDFStream
*/
/**
* Module dependencies.
*/
var Readable = require('stream').Readable;
var util = require('util');
/**
* 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
* @api public
*/
var PDFStream = module.exports = function PDFStream(canvas) {
if (!(this instanceof PDFStream)) {
throw new TypeError("Class constructors cannot be invoked without 'new'");
}
Readable.call(this);
this.canvas = canvas;
};
util.inherits(PDFStream, Readable);
function noop() {}
PDFStream.prototype._read = function _read() {
// For now we're not controlling the c++ code's data emission, so we only
// call canvas.streamPDFSync once and let it emit data at will.
this._read = noop;
var self = this;
self.canvas.streamPDFSync(function(err, chunk, len){
if (err) {
self.emit('error', err);
} else if (len) {
self.push(chunk);
} else {
self.push(null);
}
});
};