123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- '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;
|