From 8501d5d280fc63160a4315ec40ab0046fa0642e0 Mon Sep 17 00:00:00 2001 From: fethi Date: Thu, 1 Aug 2019 22:07:19 +0300 Subject: [PATCH] print table object --- README.md | 6 +++ package.json | 2 +- printer.js | 149 +++++++++++++++++++++++++++++++++++++++++++++++++++ test/test.js | 47 ++++++++++++++++ 4 files changed, 203 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 66e0d26..ad9bf5f 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,12 @@ device.open(function(){ .text('The quick brown fox jumps over the lazy dog') .text('敏捷的棕色狐狸跳过懒狗') .barcode('1234567', 'EAN8') + .table(["One", "Two", "Three"]) + .tableCustom([ + { text:"Left", align:"LEFT", width:0.33 }, + { text:"Center", align:"CENTER", width:0.33}, + { text:"Right", align:"RIGHT", width:0.33 } + ]) .qrimage('https://github.com/song940/node-escpos', function(err){ this.cut(); this.close(); diff --git a/package.json b/package.json index ace850b..62361ad 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "escpos", - "version": "2.5.0", + "version": "2.5.1", "description": "ESC/POS Printer driver for nodejs", "main": "index.js", "scripts": { diff --git a/printer.js b/printer.js index 2a8cc0b..feb73aa 100644 --- a/printer.js +++ b/printer.js @@ -103,6 +103,15 @@ Printer.prototype.println = function (content) { return this.print(content + _.EOL); }; +/** + * [function print pure content with End Of Line] + * @param {[String]} content [mandatory] + * @return {[Printer]} printer [the escpos printer instance] + */ +Printer.prototype.newLine = function () { + return this.print( _.EOL); +}; + /** * [function Print encoded alpha-numeric text with End Of Line] * @param {[String]} content [mandatory] @@ -113,6 +122,146 @@ Printer.prototype.text = function (content, encoding) { return this.print(iconv.encode(content + _.EOL, encoding || this.encoding)); }; + +/** + * [function Print draw line End Of Line] + + * @return {[Printer]} printer [the escpos printer instance] + */ +Printer.prototype.drawLine = function () { + + + // this.newLine(); + for (var i = 0; i < 48; i++) { + this.buffer.write(Buffer.from("-")); + } + this.newLine(); + + return this; +}; + + + +/** + * [function Print table with End Of Line] + * @param {[List]} data [mandatory] + * @param {[String]} encoding [optional] + * @return {[Printer]} printer [the escpos printer instance] + */ +Printer.prototype.table = function (data, encoding) { + + + var cellWidth = 48 / data.length; + var lineTxt = ""; + + for (var i = 0; i < data.length; i++) { + + lineTxt += data[i].toString(); + + var spaces = cellWidth - data[i].toString().length; + for (var j = 0; j < spaces; j++) { + lineTxt += " "; + + } + + } + this.buffer.write(iconv.encode(lineTxt+_.EOL , encoding || this.encoding)); + + return this; + + + + }; + + + +/** + * [function Print custom table with End Of Line] + * @param {[List]} data [mandatory] + * @param {[String]} encoding [optional] + * @return {[Printer]} printer [the escpos printer instance] + */ +Printer.prototype.tableCustom = function (data, encoding) { + + var cellWidth = 48 / data.length; + var secondLine = []; + var secondLineEnabled = false; + var lineStr=""; + for (var i = 0; i < data.length; i++) { + var tooLong = false; + var obj = data[i]; + obj.text = obj.text.toString(); + + if (obj.width) { + cellWidth = 48 * obj.width; + } else if (obj.cols) { + cellWidth = obj.cols + } + + + // If text is too wide go to next line + if (cellWidth < obj.text.length) { + tooLong = true; + obj.originalText = obj.text; + obj.text = obj.text.substring(0, cellWidth - 1); + } + + if (obj.align == "CENTER") { + var spaces = (cellWidth - obj.text.toString().length) / 2; + for (var j = 0; j < spaces; j++) { + lineStr +=" "; + } + if (obj.text != '') + lineStr +=obj.text; + + for (var j = 0; j < spaces - 1; j++) { + lineStr +=" "; + } + + } else if (obj.align == "RIGHT") { + var spaces = cellWidth - obj.text.toString().length; + for (var j = 0; j < spaces; j++) { + lineStr +=" "; + } + if (obj.text != '') + lineStr +=obj.text; + + } else { + if (obj.text != '') + lineStr +=obj.text; + + var spaces = cellWidth - obj.text.toString().length; + for (var j = 0; j < spaces; j++) { + lineStr +=" "; + } + + } + + + + if (tooLong) { + secondLineEnabled = true; + obj.text = obj.originalText.substring(cellWidth - 1); + secondLine.push(obj); + } else { + obj.text = ""; + secondLine.push(obj); + } + } + this.buffer.write(iconv.encode(lineStr+_.EOL , encoding || this.encoding)); + + // Print the second line + if (secondLineEnabled) { + this.tableCustom(secondLine); + } + else{ + return this; + } + + }; + + + /** * [function Print encoded alpha-numeric text without End Of Line] * @param {[String]} content [mandatory] diff --git a/test/test.js b/test/test.js index 5fd1104..eefe72b 100644 --- a/test/test.js +++ b/test/test.js @@ -20,4 +20,51 @@ describe('ESC/POS printing test', function() { printer.print('hello world').flush(); }) + it('printer#print2', function(done){ + + const networkDevice = new escpos.Network('192.168.1.87', 9100); + + + const options = { encoding: "windows1254" /* default */ } + // encoding is optional + + const printer = new escpos.Printer(networkDevice, options); + + + + networkDevice.open(function(){ + printer.tableCustom([ // Prints table with custom settings (text, align, width, cols,) + { text:"Left", align:"LEFT", width:0.33 }, + { text:"Large Message for New Line ", align:"LEFT", width:0.33 }, + { text:"Right", align:"RIGHT", width:0.33 } + ]); + printer.tableCustom([ // Prints table with custom settings (text, align, width, cols) + { text:"Left", align:"LEFT", width:0.13 }, + { text:"Left", align:"LEFT", width:0.10 }, + { text:"Center", align:"CENTER", width:0.33 }, + { text:"Right", align:"RIGHT", width:0.33 } + ]); + printer.drawLine(); + printer.tableCustom([ // Prints table with custom settings (text, align, width, cols) + { text:"Left", align:"LEFT", width:0.33 }, + { text:"Center", align:"CENTER", width:0.33}, + { text:"Right", align:"RIGHT", width:0.33 } + ]); + printer.tableCustom([ // Prints table with custom settings (text, align, width, cols) + { text:"Left", align:"LEFT", width:0.33 }, + { text:"Center", align:"CENTER", width:0.33 }, + { text:"Right", align:"RIGHT", width:0.33 } + ]); + printer.newLine(); + printer.newLine(); + printer.newLine(); + printer.newLine(); + printer.cut(); + printer.close(); + printer.beep(); + + done(); + }); + }) + }); \ No newline at end of file