Compute column widths in a HTML-like manner (based on cell contents)

Posted by cipak on Stack Overflow See other posts from Stack Overflow or by cipak
Published on 2010-06-16T08:46:44Z Indexed on 2010/06/16 9:02 UTC
Read the original article Hit count: 230

Filed under:
|
|
|
|

Hi,

I have a grid of data that I want to export to RTF, PDF etc. using various (and not perfect) PHP converters/generators.

What I am missing most is the HTML table automatic adjustment of column widths based on the lengths of strings in the cells (strings contain line breaks which complicate things a bit, as they should be preserved).

I need an algorithm that, given the contents of the cells (plain text), a total width of the table and an average width of a character, would return a width for each column. I wouldn't want to reinvent the wheel if something is already available.

Of course it can't be perfect if the font is variable width, but an approximation would do just fine. Or maybe it could have a configurable table with widths for each character.

Any hint would be appreciated. Thank you.

© Stack Overflow or respective owner

Related posts about php

Related posts about algorithm