Printing barcodes with QZ-Tray

时间:2016-10-20 13:18:43

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

I'm trying to print barcode with QZ-Tray however I can't seem to find a good example for this, I've tried the code from here https://groups.google.com/forum/#!topic/qz-print/5ybFBj4S9LA where is starts with this code:

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

However browser throws error that qz.appendHex is not a function etc.

Here's my code which can print, but just RAW data:

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

What can I do for this code to print a barcode?

3 个答案:

答案 0 :(得分: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库。

答案 1 :(得分:2)

  

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

答案 2 :(得分:0)

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