node-lp
Version:
node-lp is an adapter to the unix lp(1) command allowing files to be subitted for printing or altering a pending job.
73 lines (61 loc) • 1.97 kB
JavaScript
var Spawn = require('./spawn-lp')
, mkdirp = require('mkdirp')
, fs = require('fs')
;
module.exports.createPrinter = function(options) {
/** options are:
* `destination` - Prints files to the named printer.
* `hostname` - Chooses an alternate server.
* `port` - Chooses an alternate server port (only use if hostname is specified).
* `username` - Specifies the username to use when connecting to the server.
* `encryption` - bool, default false
* `digitalCopy` - Allows logging of what excatly is being printed.
* `debug` - bool, default false
*/
options = options || {};
// maybe use underscore?
// or a new module for this
options.destination = options.destination || false;
options.hostname = options.hostname || false;
options.port = options.port || false;
options.username = options.username || false;
options.encryption = options.encryption || false;
options.digitalCopy = options.digitalCopy || false;
options.digitalCopyDir = options.digitalCopyDir || process.env.TMP || process.env.TMPDIR || process.env.TEMP || '/tmp' || process.cwd();
options.debug = options.debug || false;
options.args = options.args || [];
var self = {}
, spawn = new Spawn(options)
;
// sends the file/buffer to `lp`
function queue(data, cb) {
var dir = options.digitalCopyDir + '/node-lp/log/digitalCopy/'
, filename = Date.now() + '.pdf'
if (options.digitalCopy) {
mkdirp(dir, function (err) {
if (err) {
return cb && cb(err);
}
fs.writeFile(dir + filename, data, function(err) {
if (err) {
return cb && cb(err);
}
});
});
}
if (Buffer.isBuffer(data)) {
spawn.withData(data, cb);
} else {
spawn.withFile(data, cb);
}
}
/**
* @deprecated
*/
function queueFile(file, cb) {
queue(file, cb);
}
self.queue = queue;
self.queueFile = queueFile;
return self;
};