Epson thermal printer javascript

elachere picture elachere · Jan 28, 2016 · Viewed 9.3k times · Source

I'm trying to use an Epson TM-T20II (thermal printer) via a web page. I've done the necessary configuration and the printer works fine on my network using the given software. So I downloaded the JS epos print SDK, and tried to run this sample code :

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
    <title>TITLE</title>
    <script type="text/javascript" src="../ePOS-Print_SDK/ePOS-Print_SDK_150729E/JavaScript/epos-print-5.0.0.js"></script>
    <script type="text/javascript">
         function buildMessage() {
             //Create an ePOS-Print Builder object
             var builder = new epson.ePOSBuilder();
             //Create a print document
             builder.addTextLang('en')
             builder.addTextSmooth(true);
             builder.addTextFont(builder.FONT_A);
             builder.addTextSize(3, 3);
             builder.addText('Hello,\tWorld!\n');
             builder.addCut(builder.CUT_FEED);
             //Acquire the print document
             var request = builder.toString();
             var address = 'http://192.168.1.65/cgi-bin/epos/service.cgi?devid=99&timeout=1000';
             //Create an ePOS-Print object
             var epos = new epson.ePOSPrint(address);
             epos.onreceive = function (res) {
             //When the printing is not successful, display a message
             if (!res.success) {
                alert('A print error occurred');
                }
             }
             //Send the print document
             epos.send(request);
         }
    </script>
    </head>
    <body>
     <button onclick='buildMessage()'>Run</button>
    </body>
</html>

For the devid parameter I tried 'local_printer' which is the name of the device, then I saw in the configuration panel that the printer id is 99. Still, it doesn't work, I got a 405 Method not allowed status code on the cgi request.

Any tips ? Thanks in advance.

EDIT :

So, after some research, it seems that the problem comes from a CORS request. The request is preflight for security matters, and this preflight request doesn't pass acces control cause the 'Access-Control-Allow-Origin' header is missing from the response. So how do I set this header ?

Answer

peterholcomb picture peterholcomb · Aug 4, 2016

With your edit mentioning a CORS issue, perhaps you can try running Chrome with web-security disabled. That should tell chrome to ignore the failing CORS header:

chromium-browser --disable-web-security

https://www.chromium.org/developers/how-tos/run-chromium-with-flags