Last active
August 2, 2020 18:18
-
-
Save musahaidari/8081c4c58e8740cba875731634bed94c to your computer and use it in GitHub Desktop.
Printing Dari/Persian/Arabic with thermal printers in node.
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
/** | |
* @typedef {object} printer | |
* @property {string} name | |
* @property {string} name_dari | |
*/ | |
/** | |
* @typedef {object} dbOrderItem | |
* @property {number} order_id | |
* @property {string} table_code | |
* @property {string} name_dari | |
* @property {number} count | |
* @property {string} notes | |
* @property {string} printer | |
* @property {string} printer_dari | |
* } | |
*/ | |
/** | |
* @typedef transformedOrder | |
* @property {printer} printer | |
* @property {string} printText | |
*/ | |
/* | |
Sample config.js: | |
module.exports = { | |
print: { | |
pullIntervalInSeconds: 1, | |
lineHeight: 35, | |
width: 600, | |
lineCharactersCount: 30, | |
endLineCharacter: '\n' | |
}, | |
db: { | |
host: '127.0.0.1', | |
database: 'xyz', | |
user: 'root', | |
password: '' | |
}, | |
checks: { | |
dumpIntervalInMinutes: 5 | |
} | |
}; | |
*/ | |
/* | |
date-time.js: | |
function now() { | |
const date = new Date(); | |
return date.getFullYear() + '-' + | |
(date.getMonth() + 1) + '-' + | |
date.getDate() + ' ' + | |
date.getHours() + ':' + | |
date.getMinutes() + ':' + | |
date.getSeconds(); | |
} | |
module.exports = { | |
now: now | |
}; | |
*/ | |
const importedThermalPrinter = require('node-thermal-printer').printer; | |
const thermalPrinterTypes = require('node-thermal-printer').types; | |
const printer = require('printer'); | |
const config = require('../config'); | |
const dateTime = require('./date-time'); | |
/** | |
* @param printerInfo {printer} | |
* @param ordersText contains string | |
*/ | |
async function order(printerInfo, ordersText) { | |
const printingText = | |
'\n******************\n' + | |
'پرنتر: ' + printerInfo.name_dari + | |
'\nزمان: ' + dateTime.now() + '\n' + | |
'\n******************\n' + ordersText; | |
const fetchedPrinter = printer.getPrinter(printerInfo.name); | |
const createCanvas = require('canvas').createCanvas; | |
const textLinesCount = printingText.split(config.print.endLineCharacter).length; | |
canvas = createCanvas( | |
config.print.width, | |
config.print.lineHeight * textLinesCount | |
); | |
const ctx = canvas.getContext("2d"); | |
ctx.textAlign = "left"; | |
ctx.font = "30px bold"; | |
ctx.fillText(printingText, 0, 50); | |
const thermalPrinter = new importedThermalPrinter({ | |
type: thermalPrinterTypes.EPSON, | |
}) | |
await thermalPrinter.printImageBuffer(canvas.toBuffer()); | |
thermalPrinter.drawLine(); | |
thermalPrinter.partialCut(); | |
printer.printDirect({ | |
data: thermalPrinter.getBuffer(), | |
printer: fetchedPrinter.name, | |
type: "RAW", | |
success: function (job_id) { | |
console.log('OK :' + job_id); | |
}, | |
error: function (error) { | |
// TODO implement file logging | |
console.error(error); | |
} | |
}); | |
} | |
module.exports = { | |
order: order | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment