schoero / swissqrbill

Swiss QR Bill generation in Node.js and browsers
MIT License
155 stars 29 forks source link

How can I right-align text in a table? #413

Closed felixmerz00 closed 10 months ago

felixmerz00 commented 11 months ago

Hello,

Thank you very much for creating SwissQRBill. I love this package. I have a question tough. I can't figure out how to right-align the text in a specific column of a table. I would like to right-align all values in the column "Price". I use TypeScript. This is the first row in my table.

{
  fillColor: '#ECF0F1',
  columns: [
    {
      text: 'Product',
      width: mm2pt(15),
    },
    {
      text: 'Price',
    },
  ],
};

I tried using the attribute align: 'right'. But this has no effect. Defining the absolute position based on the page width didn't work either. How can I right-align text in specific columns in my table?

Kind regards, Felix

schoero commented 11 months ago

Hi,

Thank you for you kind feedback. You can use textOptions: { align: "right" } for that.

However, I noticed that there is an error in the calculation of the column width, where the padding is not subtracted. So by default, the text overflows the column by the width of the padding, which is 5 by default.

As a workaround, you can set the left padding to a negative number of the padding that you actually want for that column. If you didn't set a padding manually, this would be padding: [undefined, undefined, undefined, -5].

{
  rows: [
    {
      fillColor: '#ECF0F1',
      columns: [
        {
          text: 'Product',
          width: mm2pt(15),
        },
        {
          text: 'Price',
          textOptions: { align: "right" },
          padding: [undefined, undefined, undefined, -5]
        },
      ],
    }
  ]
};

Here are two images to illustrate what i mean:

Without the workaround:

Screenshot 2023-08-28 at 19 49 52

With negative padding:

Screenshot 2023-08-28 at 19 50 18

Unfortunately I don't wan't to publish a fix for this issue, because at this point, it would most certainly be a breaking change for some users who have already worked around this issue. And for the next major release, this is already fixed.

schoero commented 10 months ago

I'm going to close this issue, as the question seems to be answered.