NielsLeenheer / ReceiptPrinterEncoder

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

Width not applied correctly when creating a table #30

Closed cervantes-x closed 2 months ago

cervantes-x commented 2 months ago

Since version 3, when attempting to reset the width value to 1 and create a table afterwards, the table continues to use the previously set width value. However, if some text is created before the table, the width resets correctly.

Code example (Not working):

const encoder = new ReceiptPrinterEncoder({
          columns: 48,
          printerModel: "pos-5890",
        });

        const result = encoder
          .initialize()
          .codepage("windows1252")
          .align("center");

        const firstColumnWidth = Math.round(width * 0.1);
        const secondColumnWidth = Math.round(width * 0.5);

        result.width(2).line("-".repeat(width / 2));

        // Attempt to reset width to 1 (this does NOT work)
        result.width(1);

        result.table(
          [
            { width: firstColumnWidth, marginRight: 1, align: "right" },
            { width: secondColumnWidth, align: "left" },
            {
              width: width - firstColumnWidth - secondColumnWidth - 1,
              align: "right",
            },
          ],
          [
            ["1x", "Spaghetti Bolognese", "€ 10,00"],
            ["10x", "Spagetti alla carbonara", "€ 100,00"],
          ],
        );

        const encoded = result.encode();

        sendToPrinter(encoded, printer);
      });

Code example (Working):

const encoder = new ReceiptPrinterEncoder({
          columns: 48,
          printerModel: "pos-5890",
        });

        const result = encoder
          .initialize()
          .codepage("windows1252")
          .align("center");

        const firstColumnWidth = Math.round(width * 0.1);
        const secondColumnWidth = Math.round(width * 0.5);

        result.width(2).line("-".repeat(width / 2));

        // Reset width to 1 and create some text (this works)
        result.width(1);
        result.text("");

        result.table(
          [
            { width: firstColumnWidth, marginRight: 1, align: "right" },
            { width: secondColumnWidth, align: "left" },
            {
              width: width - firstColumnWidth - secondColumnWidth - 1,
              align: "right",
            },
          ],
          [
            ["1x", "Spaghetti Bolognese", "€ 10,00"],
            ["10x", "Spagetti alla carbonara", "€ 100,00"],
          ],
        );

        const encoded = result.encode();

        sendToPrinter(encoded, printer);
      });

Expected Behavior:

The width value should be set to 1 and applied to the table even when no text is created beforehand.

Actual Behavior:

The width is not reset when directly creating the table after setting width(1). It only resets correctly if some text is created before the table.

NielsLeenheer commented 2 months ago

Should be fixed in release 3.0.1