【问题标题】:Printing barcodes with QZ-Tray使用 QZ-Tray 打印条码
【发布时间】:2016-10-20 13:18:43
【问题描述】:

我正在尝试使用QZ-Tray 打印条形码,但是我似乎找不到一个很好的例子,我已经尝试了这里的代码https://groups.google.com/forum/#!topic/qz-print/5ybFBj4S9LA 以该代码开头:

qz.appendHex('x1Bx40'); // init 

但是浏览器会抛出 qz.appendHex is not a function 等错误。

这是我的代码,可以打印,但只能打印 RAW 数据:

function printBarcode() {
    console.log("Print barcode");
    var config = getUpdatedConfig();
    var data = [
        'Raw Data\n',
        'More Raw Data\n',
        'Even More Raw Data\n'
    ];
    qz.print(config, data).catch(function(e) { console.error(e); });
}

我可以用这个代码做什么来打印条形码?

【问题讨论】:

    标签: javascript printing thermal-printer barcode-printing qz-tray


    【解决方案1】:

    qz.appendHex 不是函数等。

    这是 QZ Tray 1.9 的旧代码。我是changed in 2.x

    解决方案因您的打印机功能而异,但这应该可以帮助您入门。请参考ESC/P programming guide 了解更多用法。

    //barcode data
    var code = '12345';
    
    //convenience method
    var chr = function(n) { return String.fromCharCode(n); };
    
    var barcode = '\x1D' + 'h' + chr(80) +   //barcode height
        '\x1D' + 'f' + chr(0) +              //font for printed number
        '\x1D' + 'k' + chr(69) + chr(code.length) + code + chr(0); //code39
    
    qz.websocket.connect().then(function() {
       var config = qz.configs.create("Epson TM88V");
       return qz.print(config, ['\n\n\n\n\n' + barcode + '\n\n\n\n\n']);
    }).catch(function(err) { alert(err); });
    

    【讨论】:

    • 感谢@qz-support,尽管我们已经有一个有效的条形码打印代码,如我的回答中所述,我们将尝试一下,顺便说一句,您是否也可以帮助解决其他问题:stackoverflow.com/questions/40168017/…
    【解决方案2】:

    这是对我有用的代码:

    // Print Barcode
    
        function textToBase64Barcode(text){
            var canvas = document.createElement("canvas");
            JsBarcode(canvas, text, {format: "CODE39"});
            return canvas.toDataURL("image/png");
        }
        function printBarcode() {
            console.log("Print barcode");
            var config = getUpdatedConfig();
            var value = $("#barcode_num").val(); // change this with barcode number
            var base64 = textToBase64Barcode(value);
            var barcodeImg = base64.split(",")[1];
            var printData = [
                {
                    type: 'image',
                    format: 'base64',
                    data: barcodeImg
                }
            ];
            qz.print(config, printData).catch(displayError);
        }
        // End Print Barcode
    

    使用JsBarcode javascript 库。

    【讨论】:

      【解决方案3】:

      @xybrek 或 @tresf 你知道如何将你的答案与 ESC/POS 原始打印结合起来吗?

      下面使用原始图像,从 JsBarcode 怎么样?我一直在使用 JsBarcode。我可以创建条形码,但是当我尝试将其与原始数据结合时,它不起作用。我的Qz版本是2.1

      var config = qz.configs.create("Printer Name");
      
      var data = [
         { type: 'raw', format: 'image', data: 'assets/img/image_sample_bw.png', options: { language: "ESCPOS", dotDensity: 'double' } },
         '\x1B' + '\x40',          // init
         '\x1B' + '\x61' + '\x31', // center align
         'Beverly Hills, CA  90210' + '\x0A',
         '\x0A',                   // line break
         'www.qz.io' + '\x0A',     // text and line break
         '\x0A',                   // line break
         '\x0A',                   // line break
         'May 18, 2016 10:30 AM' + '\x0A',
         '\x0A',                   // line break
         '\x0A',                   // line break    
         '\x0A',
         'Transaction # 123456 Register: 3' + '\x0A',
         '\x0A',
         '\x0A',
         '\x0A',
         '\x1B' + '\x61' + '\x30', // left align
         'Baklava (Qty 4)       9.00' + '\x1B' + '\x74' + '\x13' + '\xAA', //print special char symbol after numeric
         '\x0A',
         'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' + '\x0A',       
         '\x1B' + '\x45' + '\x0D', // bold on
         'Here\'s some bold text!',
         '\x0A',
         '\x1B' + '\x45' + '\x0A', // bold off
         '\x1D' + '\x21' + '\x11', // double font size
         'Here\'s large text!',
         '\x0A',
         '\x1D' + '\x21' + '\x00', // standard font size
         '\x1B' + '\x61' + '\x32', // right align
         '\x1B' + '\x21' + '\x30', // em mode on
         'DRINK ME',
         '\x1B' + '\x21' + '\x0A' + '\x1B' + '\x45' + '\x0A', // em mode off
         '\x0A' + '\x0A',
         '\x1B' + '\x61' + '\x30', // left align
         '------------------------------------------' + '\x0A',
         '\x1B' + '\x4D' + '\x31', // small text
         'EAT ME' + '\x0A',
         '\x1B' + '\x4D' + '\x30', // normal text
         '------------------------------------------' + '\x0A',
         'normal text',
         '\x1B' + '\x61' + '\x30', // left align
         '\x0A' + '\x0A' + '\x0A' + '\x0A' + '\x0A' + '\x0A' + '\x0A',
         '\x1B' + '\x69',          // cut paper (old syntax)
      // '\x1D' + '\x56'  + '\x00' // full cut (new syntax)
      // '\x1D' + '\x56'  + '\x30' // full cut (new syntax)
      // '\x1D' + '\x56'  + '\x01' // partial cut (new syntax)
      // '\x1D' + '\x56'  + '\x31' // partial cut (new syntax)
         '\x10' + '\x14' + '\x01' + '\x00' + '\x05',  // Generate Pulse to kick-out cash drawer**
                                                      // **for legacy drawer cable CD-005A.  Research before using.
                                                      // see also http://keyhut.com/popopen4.htm
      ];
      
      qz.print(config, data).catch(function(e) { console.error(e); });
      

      【讨论】:

      • 你应该把它作为一个 SO 问题而不是一个答案发布并用 QZ 标记它
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      相关资源
      最近更新 更多