Aymkdn / html-to-pdfmake

This module permits to convert HTML to the PDFMake format
https://aymkdn.github.io/html-to-pdfmake/index.html
MIT License
560 stars 88 forks source link

Table autosize issues with percentage width and colspan #151

Closed daniel-johnsson closed 2 years ago

daniel-johnsson commented 2 years ago

First of all thanks for a great package, this really is a lifesaver.

I found some issues when tinkering around with tables, I already have fixed these issues manually with some pre/post-processing. Just wanted to report them so they might be resolved in a nicer fashion.

If the width is set to a percentage it will be set to auto if colspan is set:

With colspan

<table>
  <tr>
    <td style="width: 20%" colspan="1">Cell A1</td>
    <td>Cell A2</td>
  </tr>
</table>

"widths": ["auto", "auto"]

Without colspan

<table>
  <tr>
    <td style="width: 20%">Cell A1</td>
    <td>Cell A2</td>
  </tr>
</table>

"widths": ["20%", "auto"]

I assume that it breaks on this line and is converted to auto

Second issue is that auto is only converted too * if all widths are auto. I would assume that we want to convert all auto to a format that works in pdfMake.

My suggestion would be to change the code here Replaced with:

if (fullWidth) tableWidths=tableWidths.map(function() { return w==='auto' ? '*' : w; });

Let me know if I can assist in any way :)

Aymkdn commented 2 years ago

Thanks for the suggestion. I'll need time to review it.

As you saw, dealing with tables is quite complex 😅

Aymkdn commented 2 years ago

If the width is set to a percentage it will be set to auto if colspan is set

Actually, only the HTML attribute width is really supported when there is a colspan. Dealing with the width+colspan is difficult… So I won't change the support until someone really asks for a specific example.

In the meantime, for your case, I changed the code to ignore colspan < 2 (not available in current version as it's a very minor change and it will only be released with the next one).

I would assume that we want to convert all auto to a format that works in pdfMake.

I'm not sure to understand what you mean? auto is a valid value for PDFMake: image

daniel-johnsson commented 2 years ago

Yeah sorry for the bad phrasing, auto is valid. What I meant was that, from what I can understand if the table is set to 100% width and all cells are auto they will be converted to *. However if one of the columns is a fixed width, this will not happen and the table will not scale up to 100% width.

<table style="width:100%">
    <tr>
      <td style="width:50px">Cell A1</td>
      <td>Cell A1</td>
    </tr>
  </table>
Screenshot 2022-10-10 at 11 12 47
Aymkdn commented 2 years ago

Ah OK I understand now. I've just published the v2.4.7 with the changes.

Thanks