skip to Main Content

I have a thermal printer that I am printing labels to via a simple Java application. The printer is controlled using ZPL programming language. A string is prepared to format the label, and then this string is sent to the IP address of the printer using writeBytes(s);

I want to do the same thing but from a web page. I just need a "Print label" link that sends a simple string to the IP address of the printer causing it to spit out the relevant label. Is it possible to send raw data to an IP address in this way just by using HTML ?

Alternatively is there a simple JS method of achieving this ?

2

Answers


  1. Is it possible to send raw data to an IP address in this way just by using HTML ?

    No.

    Alternatively is there a simple JS method of achieving this ?

    No.

    The web platform (i.e. browsers at large) don’t allow sending raw data to an arbitrary IP address.

    You could write a separate (server) application that speaks e.g. HTTP(S) and/or WebSockets and relays the data to the printer, and have your page speak with that server, if you need to be able to talk to the thermal printer from a browser.

    Login or Signup to reply.
  2. You can’t print a label by using pure HTML, but you can do that using JS or PHP and a socket connection.
    The following is an example in PHP

    /* assigning port number and timeout to the socket */
    $port = 9100;
    $sec = 5;
    $usec = 0;
    $printer_ip = /* the printer IP address */
    
    $socket = socket_create(AF_INET, SOCK_STREAM, 0) or die("Could not create socketn");
    socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec'=>$sec, 'usec'=>$usec));
    socket_set_option($socket, SOL_SOCKET, SO_SNDTIMEO, array('sec'=>$sec, 'usec'=>$usec));
    socket_connect($socket, $printer_ip, $port);
    
    $command = /* your ZPL label */
    
    socket_write($socket, $command, strlen($command)) or die ("Host unreachable");
    socket_close($socket);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search