在 JavaScript 中使用多台打印机

问题描述 投票:0回答:1

我需要使用网络应用程序打印标签和收据。我使用的是 vanilla js。在打印之前,我需要一种在js函数中指定打印机的方法。我怎样才能最好地做到这一点?或者我是否可以有一些东西可以让我在客户端打印它。我找到了 qz 托盘,但我没有 499 美元的许可证,每次发送打印请求时都会有很多弹出窗口。

javascript point-of-sale posprinter qz-tray
1个回答
0
投票

在打印之前,我需要一种在js函数中指定打印机的方法。

基本语法是:

var config = qz.configs.create("My POS Printer");
qz.websocket.connect().then(() => {
   return qz.printers.find("My POS Printer");
}).then(printer => {
   var config = qz.configs.create(printer /*, additional settings */);
   var data = [ "Hello world\n\n\n\n\n" ];
   qz.print(config, data);
})

如果您不确定要使用哪台打印机,您可以将所有打印机添加到下拉列表中:


<body onload="listPrinters()">
    <label for="printers">Choose a printer:</label>
    <select name="printers" id="printers"></select><br>
    <button onclick="print()">Print</button>

    <script src="https://cdn.jsdelivr.net/npm/[email protected]/qz-tray.min.js"></script>
    
    <script>
    function listPrinters() {
        connect().then(() => {
            return qz.printers.find();
        }).then(printers => {
            var dropdown = document.getElementById("printers");
            for(var i in printers) {
                var option = document.createElement('option');
                var name = document.createTextNode(printers[i]);
                option.appendChild(name);
                dropdown.add(option);
            }
        }).catch(err => {
            console.error(err);
        });
    }

    function print() {
        connect().then(() => {
            var printer = document.getElementById("printers").value;
            var config = qz.configs.create(printer /*, additional options */);
            var data = [ "Hello world from QZ Tray\n\n\n\n\n"];

            return qz.print(config, data);
        }).catch(err => {
            console.error(err);
        });
    }

    // connection wrapper
    //  - allows active and inactive connections to resolve regardless
    //  - try to connect once before firing the mimetype launcher
    //  - if connection fails, catch the reject, fire the mimetype launcher
    //  - after mimetype launcher is fired, try to connect 3 more times
    function connect() {
        return new Promise(function(resolve, reject) {
            if (qz.websocket.isActive()) {  // if already active, resolve immediately
                resolve();
            } else {
                // try to connect once before firing the mimetype launcher
                qz.websocket.connect().then(resolve, function retry() {
                    // if a connect was not successful, launch the mimetime, try 3 more times
                    window.location.assign("qz:launch");
                    qz.websocket.connect({ retries: 2, delay: 1 }).then(resolve, reject);
                });
            }
        });
    }
    </script>
</body>

如果要指定多台打印机,

var config
可以是一个数组,它将循环指定的所有打印机。

我找到了 qz 托盘,但我没有

USD 499
许可证,每次您发送打印请求时都会有很多弹出窗口。

目前,使用QZ 托盘菜单 | 中提供的免费密钥和证书高级|站点经理 | “+”|创建新

同时,如果您能负担得起价格,请联系QZ,他们会做出特殊安排。

© www.soinside.com 2019 - 2024. All rights reserved.