简体   繁体   中英

How to directly print to network thermal printer using javascript?

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

  1. we don't want print preview dialog to show up.
  2. The printer will be connected over network and connected with IP address. I could not add the printer in printing options in android device as somehow epson print enabler is not able to detect the printer.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM