Skip to content

Commit

Permalink
Added custom table with text, aligment, bold and width
Browse files Browse the repository at this point in the history
  • Loading branch information
Klemen Kastelic committed Apr 18, 2016
1 parent caac54a commit 4334bd8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ printer.setTextQuadArea(); // Set text to quad area

printer.leftRight("Left", "Right"); // Prints text left and right
printer.table(["One", "Two", "Three"]); // Prints table equaly
printer.tableCustom([ // Prints table with custom settings (text, align, width, bold)
{ text:"Left", align:"LEFT", width:0.5 },
{ text:"Center", align:"CENTER", width:0.25, bold:true },
{ text:"Right", align:"RIGHT", width:0.25 }
]);

printer.code128("Code128"); // Print code128 bar code

Expand Down
6 changes: 6 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ printer.code128("barcode");
printer.leftRight("Left", "Right");
printer.table(["One", "Two", "Three", "Four"]);

printer.tableCustom([
{ text:"Left", align:"LEFT", width:0.5 },
{ text:"Center", align:"CENTER", width:0.25, bold:true },
{ text:"Right", align:"RIGHT", width:0.25 }
]);


printer.cut();
printer.execute();
41 changes: 41 additions & 0 deletions node-thermal-printer.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,47 @@ module.exports = {
}
},


// Options: text, align, width, bold
tableCustom: function(data){
var cellWidth = printerConfig.width/data.length;
for(var i=0; i<data.length; i++){
var obj = data[i];

if(obj.width) cellWidth = printerConfig.width * obj.width;
if(obj.bold) module.exports.bold(true);

if(obj.align == "CENTER"){
var spaces = (cellWidth - obj.text.toString().length) / 2;
for(var j=0; j<spaces; j++){
append(new Buffer(" "));
}
append(new Buffer(obj.text));
for(var j=0; j<spaces-1; j++){
append(new Buffer(" "));
}

} else if(obj.align == "RIGHT") {
var spaces = cellWidth - obj.text.toString().length;
for(var j=0; j<spaces; j++){
append(new Buffer(" "));
}
append(new Buffer(obj.text));

} else {
append(new Buffer(obj.text));
var spaces = cellWidth - obj.text.toString().length;
for(var j=0; j<spaces; j++){
append(new Buffer(" "));
}

}

if(obj.bold) module.exports.bold(false);

}
},

isPrinterConnected: function(exists){
if(printerConfig.interface){
var fs = require('fs');
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": "node-thermal-printer",
"version": "0.0.12",
"version": "0.0.13",
"description": "Made to work with Epson and Star thermal printers",
"main": "node-thermal-printer.js",
"scripts": {
Expand Down

0 comments on commit 4334bd8

Please sign in to comment.