2

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.

Radhika Gokani
  • 158
  • 1
  • 10
  • I don't think the browser gives that kind of access? – evolutionxbox Mar 17 '21 at 12:04
  • epson epos SDK is able to print without print dialog so I guess it should be possible. I am just not sure how – Radhika Gokani Mar 17 '21 at 12:08
  • Why not use their SDK? https://www.epson-biz.com/modules/ref_epos_sdk_js_en/index.php?content_id=2 – evolutionxbox Mar 17 '21 at 12:13
  • epson sdk have very limited customizations. Like I can't use custom font styles and when I add image the next content prints from new line – Radhika Gokani Mar 17 '21 at 12:19
  • 1
    This question is really rather too broad for SO. The answer is going to boil down to either "Make an HTTP request that is formatted the way the printer expects it and you'll need to find and read its API documentation to do that" or "The printer doesn't support HTTP requests to trigger printing, so you'll need to write a server-side proxy and have that proxy make a suitably formatted request" – Quentin Mar 17 '21 at 12:36
  • Perhaps the print result you want is to convert the printed content, which is a mixture of decorated text and images, into a single image data, and then request printing as an image. – kunif Mar 17 '21 at 14:47
  • I can do that but it would not work in android browser as it is not able to detect the network printer – Radhika Gokani Mar 18 '21 at 06:01
  • This article may be helpful. [How use thermal printer(USB/Ethernet) on android, without using vendor SDK,?](https://stackoverflow.com/q/47524508/9014308) – kunif Mar 18 '21 at 06:58

1 Answers1

1

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.

Md. A. Apu
  • 769
  • 10
  • 30