QZ托盘打印到多台打印机

QZ tray printing to multiple printers

我一直在尝试使用此处提供的示例使用 QZ 托盘打印到多台打印机 https://qz.io/wiki/2.0-Raw-Printing#promise-loop 但我收到一条错误消息 TypeError: Cannot read property 'sendData' of null

我有 rsvp-3.1.0.min.jssha-256.min.jsqz-tray.js。 我使用示例提供的完全相同的代码,但显然将打印机名称更改为我安装的名称。

如果我尝试分别打印到每台打印机(默认方式),一切正常

知道哪里出了问题,或者我是否缺少图书馆之类的东西?

谢谢

这是 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

所以我发现了问题,这是我自己的错误,我忘记启动 QZ 托盘,脚本完美运行,测试将 Pixel 作业发送到激光打印机,将 Raw 作业发送到热敏打印机。

编辑:解决方案

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);