function Adapter(){ EventEmitter.call(this); return this; }
n/a
function Console(handler){ this.handler = handler || stdout; }
n/a
function Image(pixels){ if(!(this instanceof Image)) return new Image(pixels); this.pixels = pixels; this.data = []; function rgb(pixel) { return { r: pixel[0], g: pixel[1], b: pixel[2], a: pixel[3] }; }; var self = this; for(var i=0;i<this.pixels.data.length;i+=this.size.colors){ this.data.push(rgb(new Array(this.size.colors).fill(0).map(function(_, b){ return self.pixels.data[ i + b ]; }))); }; this.data = this.data.map(function(pixel){ if(pixel.a == 0) return 0; return pixel.r !== 0xFF || pixel.g !== 0xFF || pixel.b !== 0xFF ? 1 : 0; }); }
n/a
function Network(address, port){ EventEmitter.call(this); this.address = address; this.port = port || 9100; this.device = new net.Socket(); return this; }
n/a
function Printer(adapter){ if (!(this instanceof Printer)) { return new Printer(adapter); } var self = this; EventEmitter.call(this); this.adapter = adapter; this.buffer = new Buffer(); }
n/a
function Serial(port, options){ var self = this; options = options || { baudrate: 9600, autoOpen: false }; this.device = new SerialPort(port, options); this.device.on('close', function() { self.emit('disconnect', self.device); self.device = null; }); EventEmitter.call(this); return this; }
n/a
function Adapter(){ EventEmitter.call(this); return this; }
n/a
extends = function (ctor){ // console.log(ctor); util.inherits(ctor, Adapter); return ctor; }
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
close = function () { throw new Error('NotImplementedException'); return this; }
...
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
this.close();
});
});
````
----
...
open = function () { throw new Error('NotImplementedException'); return this; }
...
// Select the adapter based on your printer type
const device = new escpos.USB();
// const device = new escpos.Network('localhost');
// const device = new escpos.Serial('/dev/usb/lp0');
const printer = new escpos.Printer(device);
device.open(function(){
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
...
write = function () { throw new Error('NotImplementedException'); return this; }
...
/**
* Fix bottom margin
* @param {[String]} size
* @return printer instance
*/
Printer.prototype.marginBottom = function(size){
this.buffer.write(_.MARGINS.BOTTOM);
this.buffer.writeUInt8(size);
return this;
};
/**
* Fix left margin
* @param {[String]} size
...
function Console(handler){ this.handler = handler || stdout; }
n/a
open = function (callback){ callback && callback(); }
...
// Select the adapter based on your printer type
const device = new escpos.USB();
// const device = new escpos.Network('localhost');
// const device = new escpos.Serial('/dev/usb/lp0');
const printer = new escpos.Printer(device);
device.open(function(){
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
...
write = function (data){ this.handler && this.handler(data); }
...
/**
* Fix bottom margin
* @param {[String]} size
* @return printer instance
*/
Printer.prototype.marginBottom = function(size){
this.buffer.write(_.MARGINS.BOTTOM);
this.buffer.writeUInt8(size);
return this;
};
/**
* Fix left margin
* @param {[String]} size
...
function Image(pixels){ if(!(this instanceof Image)) return new Image(pixels); this.pixels = pixels; this.data = []; function rgb(pixel) { return { r: pixel[0], g: pixel[1], b: pixel[2], a: pixel[3] }; }; var self = this; for(var i=0;i<this.pixels.data.length;i+=this.size.colors){ this.data.push(rgb(new Array(this.size.colors).fill(0).map(function(_, b){ return self.pixels.data[ i + b ]; }))); }; this.data = this.data.map(function(pixel){ if(pixel.a == 0) return 0; return pixel.r !== 0xFF || pixel.g !== 0xFF || pixel.b !== 0xFF ? 1 : 0; }); }
n/a
load = function (url, type, callback){ if(typeof type == 'function'){ callback = type; type = null; } getPixels(url, type, function(err, pixels){ if(err) return callback(err); callback(new Image(pixels)); }); }
n/a
toBitmap = function (density) { density = density || 24; var ld, result = []; var x, y, b, l, i; var c = density / 8; // n blocks of lines var n = Math.ceil(this.size.height / density); for (y = 0; y < n; y++) { // line data ld = result[y] = []; for (x = 0; x < this.size.width; x++) { for (b = 0; b < density; b++) { i = x * c + (b >> 3); if (ld[i] === undefined) { ld[i] = 0; } l = y * density + b; if (l < this.size.height) { if (this.data[l * this.size.width + x]) { ld[i] += (0x80 >> (b & 0x7)); } } } } } return { data: result, density: density }; }
...
*/
Printer.prototype.image = function(image, density){
if(!(image instanceof Image))
throw new TypeError('Only escpos.Image supported');
density = density || 'd24';
var n = !!~[ 'd8', 's8' ].indexOf(density) ? 1 : 3;
var header = _.BITMAP_FORMAT['BITMAP_' + density.toUpperCase()];
var bitmap = image.toBitmap(n * 8);
var self = this;
this.lineSpace(0); // set line spacing to 0
bitmap.data.forEach(function (line) {
self.buffer.write(header);
self.buffer.writeUInt16LE(line.length / n);
self.buffer.write(line);
self.buffer.write(_.EOL);
...
toRaster = function () { var result = []; var width = this.size.width; var height = this.size.height; var data = this.data; // n blocks of lines var n = Math.ceil(width / 8); var x, y, b, c, i; for (y = 0; y < height; y++) { for (x = 0; x < n; x++) { for (b = 0; b < 8; b++) { i = x * 8 + b; if (result[y * n + x] === undefined) { result[y * n + x] = 0; } c = x * 8 + b; if (c < width) { if (data[y * width + i]) { result[y * n + x] += (0x80 >> (b & 0x7)); } } } } } return { data: result, width: n, height: height }; }
...
Printer.prototype.raster = function (image, mode) {
if(!(image instanceof Image))
throw new TypeError('Only escpos.Image supported');
mode = mode || 'normal';
if (mode === 'dhdw' ||
mode === 'dwh' ||
mode === 'dhw') mode = 'dwdh';
var raster = image.toRaster();
var header = _.GSV0_FORMAT['GSV0_' + mode.toUpperCase()];
this.buffer.write(header);
this.buffer.writeUInt16LE(raster.width);
this.buffer.writeUInt16LE(raster.height);
this.buffer.write(raster.data);
return this;
};
...
function Network(address, port){ EventEmitter.call(this); this.address = address; this.port = port || 9100; this.device = new net.Socket(); return this; }
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
close = function (callback){ if(this.device){ this.device.destroy(); this.device = null; } this.emit('disconnect', this.device); callback && callback(null, this.device); return this; }
...
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
this.close();
});
});
````
----
...
open = function (callback){ var self = this; //connect to net printer by socket (port,ip) this.device.on("error", (err) => { callback && callback(err, self.device); }).connect(this.port, this.address, function(err){ self.emit('connect', self.device); callback && callback(err, self.device); }); return this; }
...
// Select the adapter based on your printer type
const device = new escpos.USB();
// const device = new escpos.Network('localhost');
// const device = new escpos.Serial('/dev/usb/lp0');
const printer = new escpos.Printer(device);
device.open(function(){
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
...
write = function (data, callback){ this.device.write(data, callback); return this; }
...
/**
* Fix bottom margin
* @param {[String]} size
* @return printer instance
*/
Printer.prototype.marginBottom = function(size){
this.buffer.write(_.MARGINS.BOTTOM);
this.buffer.writeUInt8(size);
return this;
};
/**
* Fix left margin
* @param {[String]} size
...
function Printer(adapter){ if (!(this instanceof Printer)) { return new Printer(adapter); } var self = this; EventEmitter.call(this); this.adapter = adapter; this.buffer = new Buffer(); }
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
align = function (align){ this.buffer.write(_.TEXT_FORMAT[ 'TXT_ALIGN_' + align.toUpperCase() ]); return this; }
...
const printer = new escpos.Printer(device);
device.open(function(){
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
...
barcode = function (code, type, width, height, position, font){ if(width >= 1 || width <= 255){ this.buffer.write(_.BARCODE_FORMAT.BARCODE_WIDTH); } if(height >=2 || height <= 6){ this.buffer.write(_.BARCODE_FORMAT.BARCODE_HEIGHT); } this.buffer.write(_.BARCODE_FORMAT[ 'BARCODE_FONT_' + (font || 'A').toUpperCase() ]); this.buffer.write(_.BARCODE_FORMAT[ 'BARCODE_TXT_' + (position || 'BLW').toUpperCase() ]); this.buffer.write(_.BARCODE_FORMAT[ 'BARCODE_' + ((type || 'EAN13').replace('-', '_').toUpperCase()) ]); this.buffer.write(code); return this; }
...
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
this.close();
});
});
...
cashdraw = function (pin){ this.buffer.write(_.CASH_DRAWER[ 'CD_KICK_' + (pin || 2) ]); return this.flush(); }
n/a
close = function (callback){ var self = this; return this.flush(function(){ self.adapter.close(callback); }); }
...
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
this.close();
});
});
````
----
...
control = function (ctrl){ this.buffer.write(_.FEED_CONTROL_SEQUENCES[ 'CTL_' + ctrl.toUpperCase() ]); return this; }
n/a
cut = function (part, feed){ this.feed(feed || 3); this.buffer.write(_.PAPER[ part ? 'PAPER_PART_CUT' : 'PAPER_FULL_CUT' ]); return this.flush(); }
...
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
this.close();
});
});
````
...
feed = function (n) { this.buffer.write(new Array(n || 1).fill(_.EOL).join('')); return this.flush(); }
...
/**
* [function Cut paper]
* @param {[type]} part [description]
* @return printer instance
*/
Printer.prototype.cut = function(part, feed){
this.feed(feed || 3);
this.buffer.write(_.PAPER[
part ? 'PAPER_PART_CUT' : 'PAPER_FULL_CUT'
]);
return this.flush();
};
/**
...
flush = function (callback){ var buf = this.buffer.flush(); this.adapter.write(buf, callback); return this; }
...
/**
* Send data to hardware and flush buffer
* @param {Function} callback
* @return printer instance
*/
Printer.prototype.flush = function(callback){
var buf = this.buffer.flush();
this.adapter.write(buf, callback);
return this;
};
/**
* [function print]
* @param {[String]} content [description]
* @param {[String]} encoding [description]
...
font = function (family){ this.buffer.write(_.TEXT_FORMAT[ 'TXT_FONT_' + family.toUpperCase() ]); return this; }
...
// const device = new escpos.Serial('/dev/usb/lp0');
const printer = new escpos.Printer(device);
device.open(function(){
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
...
hardware = function (hw){ this.buffer.write(_.HARDWARE[ 'HW_'+ hw ]); return this.flush(); }
n/a
image = function (image, density){ if(!(image instanceof Image)) throw new TypeError('Only escpos.Image supported'); density = density || 'd24'; var n = !!~[ 'd8', 's8' ].indexOf(density) ? 1 : 3; var header = _.BITMAP_FORMAT['BITMAP_' + density.toUpperCase()]; var bitmap = image.toBitmap(n * 8); var self = this; this.lineSpace(0); // set line spacing to 0 bitmap.data.forEach(function (line) { self.buffer.write(header); self.buffer.writeUInt16LE(line.length / n); self.buffer.write(line); self.buffer.write(_.EOL); }); // restore line spacing to default return this.lineSpace(); }
n/a
lineSpace = function (n) { if (n === undefined || n === null) { this.buffer.write(_.LINE_SPACING.LS_DEFAULT); } else { this.buffer.write(_.LINE_SPACING.LS_SET); this.buffer.writeUInt8(n); } return this; }
...
if(!(image instanceof Image))
throw new TypeError('Only escpos.Image supported');
density = density || 'd24';
var n = !!~[ 'd8', 's8' ].indexOf(density) ? 1 : 3;
var header = _.BITMAP_FORMAT['BITMAP_' + density.toUpperCase()];
var bitmap = image.toBitmap(n * 8);
var self = this;
this.lineSpace(0); // set line spacing to 0
bitmap.data.forEach(function (line) {
self.buffer.write(header);
self.buffer.writeUInt16LE(line.length / n);
self.buffer.write(line);
self.buffer.write(_.EOL);
});
// restore line spacing to default
...
marginBottom = function (size){ this.buffer.write(_.MARGINS.BOTTOM); this.buffer.writeUInt8(size); return this; }
n/a
marginLeft = function (size){ this.buffer.write(_.MARGINS.LEFT); this.buffer.writeUInt8(size); return this; }
n/a
marginRight = function (size){ this.buffer.write(_.MARGINS.RIGHT); this.buffer.writeUInt8(size); return this; }
n/a
print = function (content){ this.buffer.write(content); return this; }
...
/**
* [function println]
* @param {[String]} content [description]
* @param {[String]} encoding [description]
* @return printer instance
*/
Printer.prototype.println = function(content){
return this.print([ content, _.EOL ].join(''));
};
/**
* [function Print alpha-numeric text]
* @param {[String]} content [description]
* @param {[String]} encoding [description]
* @return printer instance
...
println = function (content){ return this.print([ content, _.EOL ].join('')); }
n/a
qrcode = function (code, version, level, size){ this.buffer.write(_.CODE2D_FORMAT.TYPE_QR); this.buffer.write(_.CODE2D_FORMAT.CODE2D); this.buffer.writeUInt8(version || 3); this.buffer.write(_.CODE2D_FORMAT[ 'QR_LEVEL_' + (level || 'L').toUpperCase() ]); this.buffer.writeUInt8(size || 6); this.buffer.writeUInt16LE(code.length); this.buffer.write(code); return this; }
n/a
qrimage = function (content, options, callback){ var self = this; if(typeof options == 'function'){ callback = options; options = null; } options = options || { type: 'png', mode: 'dhdw' }; var buffer = qr.imageSync(content, options); var type = [ 'image', options.type ].join('/'); getPixels(buffer, type, function (err, pixels) { if(err) return callback && callback(err); self.raster(new Image(pixels), options.mode); callback && callback.call(self, null, self); }); return this; }
...
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
this.close();
});
});
...
raster = function (image, mode) { if(!(image instanceof Image)) throw new TypeError('Only escpos.Image supported'); mode = mode || 'normal'; if (mode === 'dhdw' || mode === 'dwh' || mode === 'dhw') mode = 'dwdh'; var raster = image.toRaster(); var header = _.GSV0_FORMAT['GSV0_' + mode.toUpperCase()]; this.buffer.write(header); this.buffer.writeUInt16LE(raster.width); this.buffer.writeUInt16LE(raster.height); this.buffer.write(raster.data); return this; }
...
options = null;
}
options = options || { type: 'png', mode: 'dhdw' };
var buffer = qr.imageSync(content, options);
var type = [ 'image', options.type ].join('/');
getPixels(buffer, type, function (err, pixels) {
if(err) return callback && callback(err);
self.raster(new Image(pixels), options.mode);
callback && callback.call(self, null, self);
});
return this;
};
/**
* [image description]
...
size = function (width, height) { if (2 >= width && 2 >= height) { this.buffer.write(_.TEXT_FORMAT.TXT_NORMAL); if (2 == width && 2 == height) { this.buffer.write(_.TEXT_FORMAT.TXT_4SQUARE); } else if (1 == width && 2 == height) { this.buffer.write(_.TEXT_FORMAT.TXT_2HEIGHT); } else if (2 == width && 1 == height) { this.buffer.write(_.TEXT_FORMAT.TXT_2WIDTH); } } else { this.buffer.write(_.TEXT_FORMAT.TXT_SIZE); this.buffer.write(_.TEXT_FORMAT.TXT_WIDTH[(8 >= width) ? width : 8]); this.buffer.write(_.TEXT_FORMAT.TXT_HEIGHT[(8 >= height) ? height : 8]); } return this; }
...
device.open(function(){
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
this.close();
});
...
style = function (type){ switch(type.toUpperCase()){ case 'B': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF); break; case 'I': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF); break; case 'U': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON); break; case 'U2': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON); break; case 'BI': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF); break; case 'BIU': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON); break; case 'BIU2': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON); break; case 'BU': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON); break; case 'BU2': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_ON); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON); break; case 'IU': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_ON); break; case 'IU2': this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_ON); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL2_ON); break; case 'NORMAL': default: this.buffer.write(_.TEXT_FORMAT.TXT_BOLD_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_ITALIC_OFF); this.buffer.write(_.TEXT_FORMAT.TXT_UNDERL_OFF); break; } return this; }
...
const printer = new escpos.Printer(device);
device.open(function(){
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
this.close();
...
text = function (content, encoding){ return this.print(iconv.encode(content + _.EOL, encoding || 'GB18030')); }
...
device.open(function(){
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
this.close();
});
...
function Serial(port, options){ var self = this; options = options || { baudrate: 9600, autoOpen: false }; this.device = new SerialPort(port, options); this.device.on('close', function() { self.emit('disconnect', self.device); self.device = null; }); EventEmitter.call(this); return this; }
n/a
function EventEmitter() { EventEmitter.init.call(this); }
n/a
close = function (callback) { var self = this; this.device.drain(function(err) { self.device.close(); self.device = null; callback && callback(err, self.device); }); return this; }
...
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
.text('敏捷的棕色狐狸跳过懒狗')
.barcode('12345678', 'EAN8')
.qrimage('https://github.com/song940/node-escpos', function(err){
this.cut();
this.close();
});
});
````
----
...
open = function (callback){ this.device.open(callback); return this; }
...
// Select the adapter based on your printer type
const device = new escpos.USB();
// const device = new escpos.Network('localhost');
// const device = new escpos.Serial('/dev/usb/lp0');
const printer = new escpos.Printer(device);
device.open(function(){
printer
.font('a')
.align('ct')
.style('bu')
.size(1, 1)
.text('The quick brown fox jumps over the lazy dog')
...
write = function (data, callback){ this.device.write(data, callback); return this; }
...
/**
* Fix bottom margin
* @param {[String]} size
* @return printer instance
*/
Printer.prototype.marginBottom = function(size){
this.buffer.write(_.MARGINS.BOTTOM);
this.buffer.writeUInt8(size);
return this;
};
/**
* Fix left margin
* @param {[String]} size
...