NielsLeenheer / ReceiptPrinterEncoder

Create a set of commands that can be send to any receipt printer that supports ESC/POS or StarPRNT
MIT License
141 stars 13 forks source link

Table Encoding is giving an exception #11

Closed madVis closed 1 day ago

madVis commented 2 months ago

I'm trying to print a table like this:

let encoder = new EscPosEncoder({width: 48, wordWrap: true, imageMode: 'raster'});
      const tableHeaderArray = [
          {width: 20, align: 'left'},
          {width: 8, align: 'left'},
          {width: 20, align: 'right'}
      ];

      const tableBodyArray = [
          [(encoder) => encoder.bold(true).text('Name').bold(false),
              (encoder) => encoder.bold(true).text('Quantity').bold(false),
              (encoder) => encoder.bold(true).text('Description').bold(false)],
          ['Banana', '5', 'Ripe Yellow Bananas'],
          [(encoder) => encoder.bold(true).text('Cherry').bold(false),
              (encoder) => encoder.bold(true).text('250000000').bold(false),
              (encoder) => encoder.bold(true).text('Delicious Sweet Cherries').bold(false)]
      ];
      return encoder
          .initialize()
          .table(tableHeaderArray, tableBodyArray)
          .encode();

the exception i'm getting is: "Array length must be a positive integer of safe magnitude."

I've done some debugging and found out that it's failing while encoding "250000000" in the _flush() function In this function for 'left' and 'right' alignment we don't have a check for indent which is getting negative in this case

if (this._state.align == 'right') {
        this._queued.unshift((new Array(indent)).fill(0x20));
      }

Some other observations: if the alignment if center, it works fine. Also if i use a plain text string instead of bold (encoder callback function) it works fine.

It should ideally wrap it to the next line