Epson thermal printer JavaScript
Asked Answered
H

1

9

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 because the 'Access-Control-Allow-Origin' header is missing from the response. So how do I set this header?

Hayrick answered 28/1, 2016 at 15:19 Comment(4)
Did you find any solution? I also have the 405 response. I looked inside the javascript library made by epson. It seems that they try to connect via socket but the connection is not successfull. I also tried the iOS library made by epson and its working fine (its also a socket connection)Autunite
Finally I wrote a program in python, using an open source library made for all type of network thermal printer and called it in my JS script, that's the only way I found to got it working, and it works like a charm.Hayrick
@LongDuZboub what was the library you ended up using?Diligent
@Diligent python-escpos, here's a link escposHayrick
G
0

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

Guyot answered 4/8, 2016 at 20:32 Comment(1)
This is not a solution in any wayDuckboard

© 2022 - 2024 — McMap. All rights reserved.