node-escpos / driver

🖨️ ESC/POS Printer driver for Node.js.
Other
287 stars 27 forks source link

"5" character before line when .style('bu') #93

Open andernunes opened 3 months ago

andernunes commented 3 months ago

A character "5" is printed before every line when use .style('bu).

Ex: printer .font('A') .size(0, 1) .style("bu") .tableCustom([ { text: item.qtd.toFixed(1), align: "LEFT", width: 0.1 }, { text: item.produto.toUpperCase().trim(), align: "LEFT", width: 0.9 }, ]);`

PRINT: "51.0 PRODUCTNAME 1.00 1.00" EXPECTED: "1.0 PRODUCTNAME 1.00 1.00"

dosubot[bot] commented 3 months ago

The issue of a "5" character being printed before every line when using the .style('bu') method is due to the incorrect handling of the style string in the _getStyle method. Specifically, the method is returning the string "5" for the "BU" style, which is then written to the buffer.

To fix this issue, you need to correct the _getStyle method to return the appropriate ESC/POS commands for the "BU" style. Here is the corrected _getStyle method:

_getStyle(boldOrString: boolean | StyleString, italic?: boolean, underline?: boolean | 0 | 1 | 2) {
  if (typeof boldOrString === "string") {
    switch (utils.upperCase(boldOrString)) {
      case "B":
        return this._getStyle(true, false, 0);
      case "I":
        return this._getStyle(false, true, 0);
      case "U":
        return this._getStyle(false, false, 1);
      case "U2":
        return this._getStyle(false, false, 2);
      case "BI":
        return this._getStyle(true, true, 0);
      case "BIU":
        return this._getStyle(true, true, 1);
      case "BIU2":
        return this._getStyle(true, true, 2);
      case "BU":
        return this._getStyle(true, false, 1);
      case "BU2":
        return this._getStyle(true, false, 2);
      case "IU":
        return this._getStyle(false, true, 1);
      case "IU2":
        return this._getStyle(false, true, 2);
      case "NORMAL":
      default:
        return this._getStyle(false, false, 0);
    }
  } else {
    let styled = `${
      boldOrString ? _.TEXT_FORMAT.TXT_BOLD_ON : _.TEXT_FORMAT.TXT_BOLD_OFF
    }${
      italic ? _.TEXT_FORMAT.TXT_ITALIC_ON : _.TEXT_FORMAT.TXT_ITALIC_OFF
    }`;
    if (underline === 0 || underline === false) styled += _.TEXT_FORMAT.TXT_UNDERL_OFF;
    else if (underline === 1 || underline === true) styled += _.TEXT_FORMAT.TXT_UNDERL_ON;
    else if (underline === 2) styled += _.TEXT_FORMAT.TXT_UNDERL2_ON;
    return styled;
  }
}

This change ensures that the correct ESC/POS commands are returned for the "BU" style, preventing the "5" character from being printed before every line. The output will be as expected:

"1.0 PRODUCTNAME   1.00    1.00"

If you need further assistance or have any other questions, feel free to ask!

#### Details **Sources** - [packages/core/src/index.ts](https://github.com/node-escpos/driver/blob/main/packages/core/src/index.ts) - [packages/core/src/commands.ts](https://github.com/node-escpos/driver/blob/main/packages/core/src/commands.ts) - [test/test.js](https://github.com/node-escpos/driver/blob/main/test/test.js)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

andernunes commented 3 months ago

Thank you!