You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
59 lines
1.2 KiB
59 lines
1.2 KiB
'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;
|
|
|