Calculate column width in HTML way (based on cell content)

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

What I miss most is the automatic adjustment of the width of the columns of the HTML table depending on the length of the rows in the cells (the rows contain line breaks, which complicate the situation a bit, since they must be saved).

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

Of course, it cannot be perfect if the font is a variable width, but the approximation will be very good. Or maybe it can have a custom table with a width for each character.

Any hint would be appreciated.

+4
source share
1 answer

It is not simple.

In PHPExcel, when the cell is set to autowidth, we use the gmit library function imagettfbbox ()

// font size should really be supplied in pixels in GD2, // but since GD2 seems to assume 72dpi, pixels and points are the same $fontFile = self::getTrueTypeFontFileFromFont($font); $textBox = imagettfbbox($font->getSize(), $rotation, $fontFile, $text); // Get corners positions $lowerLeftCornerX = $textBox[0]; $lowerLeftCornerY = $textBox[1]; $lowerRightCornerX = $textBox[2]; $lowerRightCornerY = $textBox[3]; $upperRightCornerX = $textBox[4]; $upperRightCornerY = $textBox[5]; $upperLeftCornerX = $textBox[6]; $upperLeftCornerY = $textBox[7]; // Consider the rotation when calculating the width $textWidth = max($lowerRightCornerX - $upperLeftCornerX, $upperRightCornerX - $lowerLeftCornerX); return $textWidth; 

This is very intense, especially when working with large sheets, so we also have an alternative method (approximation)

 // Calculate column width in pixels. We assume fixed glyph width. Result varies with font name and size. switch ($fontName) { case 'Calibri': // value 8.26 was found via interpolation by inspecting real Excel files with Calibri 11 font. $columnWidth = (int) (8.26 * PHPExcel_Shared_String::CountCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size break; case 'Arial': // value 7 was found via interpolation by inspecting real Excel files with Arial 10 font. $columnWidth = (int) (7 * PHPExcel_Shared_String::CountCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size break; case 'Verdana': // value 8 was found via interpolation by inspecting real Excel files with Verdana 10 font. $columnWidth = (int) (8 * PHPExcel_Shared_String::CountCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 10; // extrapolate from font size break; default: // just assume Calibri $columnWidth = (int) (8.26 * PHPExcel_Shared_String::CountCharacters($columnText)); $columnWidth = $columnWidth * $fontSize / 11; // extrapolate from font size break; } // Calculate approximate rotated column width if ($rotation !== 0) { if ($rotation == -165) { // stacked text $columnWidth = 4; // approximation } else { // rotated text $columnWidth = $columnWidth * cos(deg2rad($rotation)) + $fontSize * abs(sin(deg2rad($rotation))) / 5; // approximation } } // pixel width is an integer $columnWidth = (int) $columnWidth; return $columnWidth; 
+2
source

Source: https://habr.com/ru/post/1312942/


All Articles