QZ TRAY PRINITING ORDER NOT IN SEQ

911 Views Asked by At

I'm trying to print qz tray from javascript. I have barcode with number in ascending order 1,2,3,4, 5 and so on. I looping the seq correctly . but when printed out, it was not in order.

    setTimeout("directPrint2()",1000);

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

function directPrint2(){
  var data;
  var xhttp;
  var v_carton = "' || x_str_carton ||'";       
  var carton_arr = v_carton.split('','');                                                                                                        
  var v1 =  "' ||
replace(x_zebra_printer_id, '\', '|') ||
'".replace(/\|/g,"\\");
  if(v1 == ""){
      alert("Please setup ZPL Printer");
  }
  else{                                                   
      xhttp=new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
          if (this.readyState == 4 && this.status == 200) {
              data = [ toNative(this.responseText) ];
              printZPL(data, v1);
          }
      };

     for (var j = 0; j < carton_arr.length; j++){                                                         

      var url = "' || x_wms_url ||
'WWW_URL.direct_print_label?in_carton_no="+toValidStr(carton_arr[j]);

      xhttp.open("GET", url, false);                                                                                                       
      xhttp.send();   
      sleep(5000);                                                                                                             
    }  
  }               
};

',
'javascript'
1

There are 1 best solutions below

0
On

What's missing from your example:

  • I do not see any looping logic in the example calling the printZPL function,
  • printZPL isn't a QZ Tray function and you're missing the code snippet which it calls. Usually this would be qz.print(config, data);.

Regardless of the missing information, the qz.print(...) API is ES6/Promise/A+ based meaning if you want to call qz.print multiple times in a row you need to use a Promise-compatible technique. (e.g. .then(...) syntax) between your print calls as explained in the Chaining Requests guide.

To avoid this, you can concatenate all ZPL data into one large data array. Be careful not to spool too much data at once.

If you know exactly how many jobs you'll be appending, you can hard-code the promise chain:

qz.websocket.connect()
.then(function() { 
   return qz.printers.find("zebra");              // Pass the printer name into the next Promise
})
.then(function(printer) {
   var config = qz.configs.create(printer);       // Create a default config for the found printer
   var data = ['^XA^FO50,50^ADN,36,20^FDRAW ZPL EXAMPLE^FS^XZ'];   // Raw ZPL
   return qz.print(config, data);
})
.catch(function(e) { console.error(e); });

Finally, if you do NOT know in advanced how many calls to qz.print(...) you can use a Promise loop as explained in the Promise Loop guide.

function promiseLoop() {
    var data = [
        "^XA\n^FO50,50^ADN,36,20^FDPRINT 1 ^FS\n^XZ\n",
        "^XA\n^FO50,50^ADN,36,20^FDPRINT 2 ^FS\n^XZ\n",
        "^XA\n^FO50,50^ADN,36,20^FDPRINT 3 ^FS\n^XZ\n",
        "^XA\n^FO50,50^ADN,36,20^FDPRINT 4 ^FS\n^XZ\n"
    ];
    var configs = [
        { "printer": "ZDesigner LP2844-Z" },
        { "printer": "ZDesigner LP2844-Z" },
        { "printer": "ZDesigner LP2844-Z" },
        { "printer": "ZDesigner LP2844-Z" }
    ];
    var chain = [];

    for(var i = 0; i < data.length; i++) {
        (function(i_) {
            //setup this chain link
            var link = function() {
                return qz.printers.find(configs[i_].printer).then(function(found) {
                    return qz.print(qz.configs.create(found), [data[i_]]);
                });
            };

            chain.push(link);
        })(i);
        //closure ensures this promise's concept of `i` doesn't change
    }

    //can be .connect or `Promise.resolve()`, etc
    var firstLink = new RSVP.Promise(function(r, e) { r(); });

    var lastLink = null;
    chain.reduce(function(sequence, link) {
        lastLink = sequence.then(link);
        return lastLink;
    }, firstLink);

    //this will be the very last link in the chain
    lastLink.catch(function(err) {
        console.error(err);
    });
}

Note: The Promise Loop is no longer needed in QZ Tray 2.1. Instead, since 2.1, an array of config objects and data arrays can be provided instead.