forked from kozmoz/node-thermal-printer
-
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.
- Abstracted printer interface - Support for printer (https://www.npmjs.com/package/printer) - Support for electron-printer (https://www.npmjs.com/package/electron-printer) - Tested with more printers - Easy to use interface configuration strings (tcp://, printer:, \\.\COM1)
- Loading branch information
Showing
6 changed files
with
204 additions
and
62 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,37 @@ | ||
var fs = require("fs"); | ||
|
||
var writeFile = require("write-file-queue")({ | ||
retries : 1000, // number of write attempts before failing | ||
waitTime : 200 // number of milliseconds to wait between write attempts | ||
//, debug : console.error // optionally pass a function to do dump debug information to | ||
}); | ||
|
||
function File(path) { | ||
this.path = path; | ||
} | ||
File.prototype.execute = function(buffer, cb) { | ||
writeFile(this.path, buffer, function (err) { | ||
if (err) { | ||
if ("function" === typeof cb) { | ||
cb("Print failed: " + err); | ||
} else { | ||
console.error("Print failed", err); | ||
} | ||
} else { | ||
if ("function" === typeof cb) { | ||
cb( null ); | ||
} else { | ||
console.log("Print done"); | ||
} | ||
} | ||
}); | ||
}; | ||
File.prototype.isPrinterConnected = function(exists){ | ||
if (this.path){ | ||
fs.exists(this.path, function(ex){ | ||
exists(ex); | ||
}); | ||
} | ||
}; | ||
|
||
module.exports = File; |
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,22 @@ | ||
const parseNet = /^tcp:\/\/([^\/:]+)(?::(\d+))?\/?$/i; | ||
const parsePrinter = /^printer:([^\/]+)(?:\/([\w-]*))?$/i; | ||
|
||
function getInterface(uri) { | ||
if (typeof uri === "object") { | ||
return uri; | ||
} | ||
const net = parseNet.exec(uri); | ||
if (net) { | ||
const Mod = require('./net'); | ||
return new Mod(net[1], net[2]); | ||
} | ||
const printer = parsePrinter.exec(uri); | ||
if (printer) { | ||
const Mod = require('./printer'); | ||
return new Mod(printer[1], printer[2]); | ||
} | ||
|
||
const Mod = require('./file'); | ||
return new Mod(uri); | ||
} | ||
module.exports = getInterface; |
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,53 @@ | ||
var net = require("net"); | ||
|
||
function NetPrint(host, port) { | ||
this.timeout = 3000; | ||
this.host = host; | ||
this.port = port || 9100; | ||
} | ||
NetPrint.prototype.execute = function(buffer, cb) { | ||
var printer = net.connect({ | ||
host : this.host, | ||
port : this.port, | ||
timeout: this.timeout | ||
}, function() { | ||
printer.write(buffer, null, function () { | ||
if (typeof cb !== "undefined") { | ||
cb(null); | ||
} | ||
printer.end(); | ||
}); | ||
}); | ||
printer.on('error', function (err) { | ||
if (typeof cb !== "undefined") { | ||
cb(err); | ||
} | ||
printer.end(); | ||
}); | ||
printer.on('timeout', function () { | ||
if (typeof cb !== "undefined") { | ||
cb("Error: Socket Timeout"); | ||
} | ||
printer.end(); | ||
}); | ||
}; | ||
NetPrint.prototype.isPrinterConnected = function(exists){ | ||
var printer = net.connect({ | ||
host : this.host, | ||
port : this.port, | ||
timeout: this.timeout | ||
}, function() { | ||
exists(true); | ||
printer.end(); | ||
}); | ||
printer.on('error', function (err) { | ||
exists(false); | ||
printer.end(); | ||
}); | ||
printer.on('timeout', function () { | ||
exists(false); | ||
printer.end(); | ||
}); | ||
}; | ||
|
||
module.exports = NetPrint; |
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,49 @@ | ||
const electron = typeof process !== 'undefined' && process.versions && !!process.versions.electron; | ||
|
||
function PrinterIface(printerName, moduleName) { | ||
this.name = printerName; | ||
if (moduleName && typeof moduleName === "object") { | ||
this.driver = moduleName; | ||
} else { | ||
this.driver = require(moduleName || (electron ? "electron-printer" : "printer")); | ||
} | ||
} | ||
PrinterIface.prototype.getPrinterName = function() { | ||
var name = this.name; | ||
if (!name || name === "auto") { | ||
const pl = this.driver.getPrinters().filter(function(p) { return p.attributes.indexOf("RAW-ONLY") > -1 }); | ||
if (pl.length > 0) { | ||
name = pl[0].name; | ||
} | ||
} | ||
if (!name || name === "auto") { | ||
throw "A RAW-ONLY Printer could not be detected. Please configure a Printer-Name"; | ||
} | ||
return name; | ||
}; | ||
PrinterIface.prototype.isPrinterConnected = function(exists){ | ||
if (this.driver.getPrinter(this.getPrinterName())) { | ||
exists(true); | ||
} else { | ||
exists(false); | ||
} | ||
}; | ||
PrinterIface.prototype.execute = function(buffer, cb) { | ||
this.driver.printDirect({ | ||
data: buffer, | ||
printer: this.getPrinterName(), | ||
type: "RAW", | ||
success: function(jobID) { | ||
if (typeof cb === "function") { | ||
cb(null); | ||
} | ||
}, | ||
error: function(err) { | ||
if (typeof cb === "function") { | ||
cb(err); | ||
} | ||
} | ||
}); | ||
}; | ||
|
||
module.exports = PrinterIface; |
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