javascriptqz-tray

QZ tray printing to multiple printers


I have been trying to print to multiple printers with QZ tray using the examples provided here https://qz.io/wiki/2.0-Raw-Printing#promise-loop but I am getting an error saying TypeError: Cannot read property 'sendData' of null

I have rsvp-3.1.0.min.js, sha-256.min.js and qz-tray.js included. I use the exact same code provided by the example but obviously changed the printers names to the ones I have installed.

Everything works fine if I try to print to each printer separately (the default way of doing it)

Any idea of what could be wrong or if I am missing a library or something?

Thank you

This is the full error message on Safari:

TypeError: Cannot read property 'sendData' of null
    at qz-tray.js:323
    at lib$rsvp$$internal$$initializePromise (rsvp-3.1.0.min.js:10)
    at new lib$rsvp$promise$$Promise (rsvp-3.1.0.min.js:10)
    at Object.promise (qz-tray.js:456)
    at Object.dataPromise (qz-tray.js:314)
    at Object.find (qz-tray.js:788)
    at link (test.html:388)
    at lib$rsvp$$internal$$tryCatch (rsvp-3.1.0.min.js:10)
    at lib$rsvp$$internal$$invokeCallback (rsvp-3.1.0.min.js:10)
    at rsvp-3.1.0.min.js:10

Solution

  • So I found the problem and it was my own mistake, I forgot to initiate QZ tray, the script works perfectly, tested sending a Pixel job to a laser printer and a Raw job to a thermal printer.

    EDIT: Solution

    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);
        });
    }
    //Always make sure you are establishing a connection before printing
    qz.websocket.connect().then(promiseLoop).catch(errorHandler);