3 Replies Latest reply on Aug 10, 2017 12:13 PM by Daniel Wiesmann

    Zebra Javascript printing status from printer

    Nick van de Luijtgaarden

      Hello guys!

       

      Using the web developing guide that Robin provided (link incoming..), I am trying to make an application so I can print some labels with user input. For now it is working fine, but if I wanna share this application with other people, so I would like there to be a button in my application so people can test their printer, because I can already predict that a lot of people are going to fill in a wrong IP or someting like that.

       

      What I would like their to be is a command so I can get get a response from the printer, and thus check if the printer is connected, it is online etc. Is there any ZPL command or other way to do this?

       

      My code:

       

              var ip_addr = "192.168.254.111"

              var url = "http://" + ip_addr + "/pstprnt";

              var async = true;

              var method = "POST";

              var zpl = "^XA^FO20,10^AD^FDTest^FS^FO20,60^B3^FDAAA001^FS^XZ";

              var request = new XMLHttpRequest();

       

              request.onload = function () {

                  var status = request.status;

                  var data = request.responseText;

              }

       

              request.open(method, url, async);

              request.send(zpl);

       

      This works fine, and I can print labels, but I do not receive any response in the status en data variable, so in my application I can never confirm if the label has been  printed.

       

      Thanks!

        • Re: Zebra Javascript printing status from printer
          Robin West

          Hi Nick,

          You've hit on one of the major drawbacks of the way this work in our printers.  The only responses you can get are simple 200 responses.  This is what you should be getting in the "var status" in the request.onload function.  You can verify something is at that address.  If you want to verify it's a zebra printer and maybe scrape some useful current status info, you can post to the printer webpage.  This is not a fail-safe and I've had issues with not getting any response on Chrome (I'm guessing it may be blocked in some circumstances)

              function verify()

              {

                var ip_addr = "192.168.254.111";

                var output = document.getElementById("output");

                var url = "http://"+ip_addr+"/index.html";

                var method = "POST";

                var async = true;

                var request = new XMLHttpRequest();

                var message = "";

           

                request.onload = function () {

                  var status = request.status; // HTTP response status, e.g., 200 for "200 OK"

                  var data = request.responseText; // Returned data, e.g., an HTML document.

                  var printStatus = data.substr(data.indexOf("Status:") - 4);

                  printStatus = printStatus.substring(0, printStatus.indexOf("</H3>") + 5);

                  var printer = data.substr(data.indexOf("Zebra Technologies") - 4);

                  var printer = printer.substring(0,printer.indexOf("/") + 4);

                  output.innerHTML = printer + printStatus;

                }

           

                request.open(method, url, async);

           

                // Actually sends the request to the server.

                request.send(message);

              }

           

          I can't guarantee this scraping will work with every printer, the pages have changed some over time, but you can work with it to get some better ideas. I'm sure there's an easier javascript or jquery way to get the correct elements.

          The lack of 2-way communication is one of the many reasons we created Browser Print. I'm still highly encouraging people to try switching to Browser Print if at all possible, as we can't guarantee if or when the HTTP post method will be blocked by different browsers for security.

           

          Robin West