最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

printing - Epson thermal printer JavaScript - Stack Overflow

programmeradmin3浏览0评论

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 es 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?

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 es 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?

Share Improve this question edited Aug 30, 2023 at 2:47 dev_light 4,2187 gold badges14 silver badges30 bronze badges asked Jan 28, 2016 at 15:19 elachereelachere 8552 gold badges10 silver badges23 bronze badges 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) – CR7 Commented Jun 18, 2016 at 20:56
  • 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. – elachere Commented Jun 19, 2016 at 16:09
  • @LongDuZboub what was the library you ended up using? – CountMurphy Commented Sep 5, 2017 at 21:10
  • 2 @CountMurphy python-escpos, here's a link escpos – elachere Commented Sep 6, 2017 at 8:08
Add a ment  | 

1 Answer 1

Reset to default 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/developers/how-tos/run-chromium-with-flags

发布评论

评论列表(0)

  1. 暂无评论