Skip to content

Commit

Permalink
print table object
Browse files Browse the repository at this point in the history
  • Loading branch information
fethisu authored and lsongdev committed Aug 20, 2019
1 parent 34eef3a commit 8501d5d
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
149 changes: 149 additions & 0 deletions printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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]
Expand Down
47 changes: 47 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
})

});

0 comments on commit 8501d5d

Please sign in to comment.