Reputation: 178
I have a requirement at work to create a pos service to print receipt to a thermal printer using javascript. The target printer is epson Tm-m30.
I generated html page with receipt details and tried printing using window.print() but
Next I tried epson epos SDK for javascript. I was able to print from all devices but the sdk have limited customization. We can't add styles like we add in html page for print.I was not able to figure out how to add custom font in SDK. Also if I add image in print then content after image prints from next line. I tried adding text in image but then extra text does not wrap in new line and get cut. So epos SDK seems too much work but still less customization.
I want to directly print html page to a network printer using printer's IP address from android device without showing print preview dialog.
Upvotes: 3
Views: 7943
Reputation: 1248
Epson has JavaScript SDK, that can print from web page, but you need to have fixed (printer) ip so you can connect using that. Because of browser rules/security measures finding printer from browser is not possible.
You can check their (respective) printer documentation for how to do it or check it here
Usually they provided bear-bone code samples, Like the following, which I've tested for Epson TM-M30.
var ePosDev = new epson.ePOSDevice();
var printer = null;
function connect() {
//Connects to a device
ePosDev.connect('192.168.192.168', '8008', callback_connect);
}
function callback_connect(resultConnect) {
if ((resultConnect == 'OK') || (resultConnect == 'SSL_CONNECT_OK')) {
//Retrieves the Printer object
ePosDev.createDevice('local_printer', ePosDev.DEVICE_TYPE_PRINTER, {
'crypto': false,
'buffer': false
}, callback_createDevice);
}
else {
//Displays error messages
}
}
function callback_createDevice(deviceObj, retcode) {
printer = deviceObj;
if (retcode == 'OK') {
printer.timeout = 60000;
//Registers an event
printer.onstatuschange = function (res) { alert(res.success); };
printer.onbatterystatuschange = function (res) { alert(res.success); };
print();
} else {
alert(retcode);
}
}
function startMonitor() {
//Starts the status monitoring process
printer.startMonitor();
}
//Opens the printer cover
function stopMonitor() {
//Stops the status monitoring process
printer.stopMonitor();
}
function disconnect() {
//Discards the Printer object
ePosDev.deleteDevice(printer, callback_deleteDevice);
}
function callback_deleteDevice(errorCode) {
//Terminates connection with device
ePosDev.disconnect();
}
There are other generic thermal printer handling packages available too, in GitHub.
Upvotes: 2