forked from Automattic/node-canvas
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters