ingoncalves / escpos-xml

JavaScript library that implements the thermal printer ESC / POS protocol and provides an XML interface for preparing templates for printing.
Apache License 2.0
48 stars 20 forks source link

QR code not printed #9

Open VyankeshH opened 6 years ago

VyankeshH commented 6 years ago

Not able to get QR Code using this.

ingoncalves commented 6 years ago

You must make sure if your printer supports the full ESC/POS protocol, and also check if the size of the data you're printing is in an acceptable range by the printer.

VyankeshH commented 6 years ago

@ingoncalves I got result with barcode but I get that String when I am trying to print QRCode of that respected String.Also I check the features of my Printer It support ESC/POS protocol.Then what is the issue I didn't understand

ingoncalves commented 6 years ago

@VyankeshH try doing this:

import { EscPos } from '@datahex/escpos-xml';

const xml = `
  <?xml version="1.0" encoding="UTF-8"?>
  <document>
    <qrcode ecl="M">{{qrcode}}</qrcode>
  </document>
`;

const data = {
  qrcode: 'hello qrcode'
};

const buffer = EscPos.getBufferFromTemplate(xml, data);
// send this buffer to the printer

Thus, we are going to eliminate other hypotheses of error. Please, report me the result.

VyankeshH commented 6 years ago

@ingoncalves If I used above code then my Output on printer will be : hello qrcode

ingoncalves commented 6 years ago

Well, in this case, I think that your printer doesn't support the QR code feature. Unfortunately, there are some models that don't support it. You should check your printer specifications.

VyankeshH commented 6 years ago

@ingoncalves OK Brother,Actually I checked all specification. It showing for the QRcode supported

ingoncalves commented 6 years ago

Hmm, it's weird. So, you may use the BufferBuilder or try writing the buffer manually, according to the printer specification. There might be differences in protocol.

shaniqwa commented 6 years ago

having the same issue, with CITIZEN CT-S310II printer which support QR printing. have tried EscPos.getBufferBuilder().printQRcode(data). all I get is the data printed instead of QR :\ any help would be much appreciated. btw great project..!

Blair2004 commented 6 years ago

I've tested as well with my printer, EPSON TM-T20II and it support the barcode and Qr Code.

VyankeshH commented 6 years ago

@shaniqwa Did you get solution for QR Code printing.

shaniqwa commented 6 years ago

@VyankeshH I have implemented this function with some help from node-thermal-printer

  /**
   *
   * @param code - string of the QR code
   * @param version - QR model: number 1, 2
   * @param level - correction level: L - 7%,  M - 15%, Q - 25%, H - 30%
   * @param size - QR size, number between 1 - 16
   * @returns {Array}
   */
  static printQR(code: string, version?: number, level?: string, size?: number): any[] {
    const buffer: any[] = [];

    // center the QR Code
    buffer.push(...config.TXT_ALIGN_CT);
    buffer.push(...config.HW_SELECT);

    // [Name] Select the QR code model
    // [Code] 1D 28 6B 04 00 31 41 n1 n2
    // n1
    // [49 x31, model 1]
    // [50 x32, model 2]
    // [51 x33, micro qr code]
    // n2 = 0
    if (version) {
      if (version === 1) {
        buffer.push(...config.QRCODE_MODEL1);
      } else {
        buffer.push(...config.QRCODE_MODEL2);
      }
    } else {
      buffer.push(...config.QRCODE_MODEL2);
    }

    // [Name]: Set the size of module
    // 1D 28 6B 03 00 31 43 n
    // n depends on the printer
    if (size) {
      const command: string = 'QRCODE_CELLSIZE_'.concat(size.toString());
      buffer.push(...config[command]);
    } else {
      buffer.push(...config.QRCODE_CELLSIZE_3);
    }

    // [Name] Select the error correction level
    // 1D 28 6B 03 00 31 45 n
    // n
    // [48 x30 -> 7%]
    // [49 x31-> 15%]
    // [50 x32 -> 25%]
    // [51 x33 -> 30%]
    if (level) {
      const command: string = 'QRCODE_CORRECTION_'.concat(level.toUpperCase());
      buffer.push(...config[command]);
    } else {
      buffer.push(...config.QRCODE_CORRECTION_M);
    }

    // [Name] Store the data in the symbol storage area
    // 1D 28  6B pL pH 31 50 30 d1...dk
    const s: number = code.length + 3;
    const pL: number = Math.floor(s % 256);
    const pH: number = Math.floor(s / 256);
    buffer.push(...[0x1d, 0x28, 0x6b, pL, pH, 0x31, 0x50, 0x30]);
    // convert Buffer to array
    const codeBuf: Buffer = new Buffer(code);
    const codeArr = Array.prototype.slice.call(codeBuf, 0);
    buffer.push(...codeArr);

    // buffer.push(...new Buffer((pL + pH * 256) - 3)); // CITIZEN PRINTER ONLY

    // [Name] Print the symbol data in the symbol storage area
    // 1D 28 6B 03 00 31 51 m
    buffer.push(...config.QRCODE_PRINT);

    return buffer;
  }
VyankeshH commented 6 years ago

@shaniqwa I tried this also still I got Chinese characters on print.My printer name is FUKUN pos80 printer.

shaniqwa commented 6 years ago

@VyankeshH you have to follow your printer's guide for QR printing and change the commands if needed. each printer is a bit different, this code works for CITIZEN printer. I have downloaded an ugly pdf with all printers commands and followed it, eventually it worked.

VyankeshH commented 6 years ago

@shaniqwa Can you show me the return data? Is it possible for you now??

VyankeshH commented 6 years ago

@shaniqwa Thank you.I got the result.

VyankeshH commented 6 years ago

@shaniqwa @ingoncalves How could I merge two buffer for printing purpose. first buffer is XML and second buffer is QRCode buffer.I want to print first buffer on left side of the page and second buffer on the Right side of the page.Any help would be much appreciated

rahuljain0114 commented 6 years ago

@VyankeshH I am facing the same problem. Could you help me how you implemented @shaniqwa solution.

tusharmutreja200 commented 3 years ago

I am also facing the same issue, is there any update

FazilMuhammed commented 2 years ago

@VyankeshH I have base64 data is it possible to get qrcode