title: How to calculate the "perfect" column widths
body: <p>So, here's the plan: I'm using TCPDF to generate PDF documents containing a table. I'm generating an html table in PHP which I pass to TCPDF. However, TCPDF makes each column's width equal, which is a problem, as the content length in each column is quite different. The solution is to set the <code>width</code> attribute on the table's <code>&lt;td&gt;</code>s. But I can't quite workout the perfect way to do so. That's what I'm currently doing:</p>
  
  <ol>
  <li>I generate an array called <code>$maxColumnSizes</code> in which I store the <strong>maximum</strong> number of letters per column.</li>
  <li>I generate an array called <code>$averageSizes</code> in which I store the <strong>average</strong> number of letters per column.</li>
  </ol>
  
  <p>So, below you see an example calculation. Column 0 has 8 letters average, and 26 letters at max, column 4 has 10 letters average, and 209 letters at max:
  <img src="https://i.stack.imgur.com/pffMM.png" alt="enter image description here"></p>
  
  <p>So, here's the problem: I can't think of the "right" way to combine this information to get the "perfect" column widths. If I ignore the <code>$maxColumnSizes</code> array and set the column widths based on the <code>$averageSizes</code>, the table looks quite good. <strong>Except for the one row where Column 4 has 209 characters. As Column 4 is pretty small, the row where there are 209 characters has an insane height, to fit the 209 characters in.</strong></p>
  
  <p>To sum it up: How do I calculate the "perfect" table column width (given the table data)?</p>
  
  <p><em>Notes:</em></p>
  
  <ul>
  <li>"perfect width" for me means that the whole table's height is as small as possible.</li>
  <li>I currently do not take letter-widths into account (I do not differentiate between the width of an <code>i</code> and a <code>w</code>)</li>
  <li>As I have access to all the data, I can also make any other calculations needed. The two arrays I mention above I only used in my first tries. </li>
  </ul>
  
  <p><strong>EDIT</strong></p>
  
  <p>Based on the comment I added another calculation calculating $maxColumnSize / $averageColumnSize:
  <img src="https://i.stack.imgur.com/RMQuA.png" alt="enter image description here"></p>