javascriptprintingthermal-printerbarcode-printingqz-tray

Printing barcodes with 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?


Solution

  • qz.appendHex is not a function etc.

    That's old code for QZ Tray 1.9. It's changed in 2.x.

    The solution varies based on your printer's capabilities, but this should get you started. Please reference the ESC/P programming guide for further usage.

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