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


I have a grid of data that I want to export in RTF, PDF etc / generator.

What I am missing is that the HTML table is automatically adjusted for the width of the column based on the length of the stars in the cells (string is a line break which makes things a little complicated, Because they should be protected).

I need an algorithm, that the contents of cells (plain text), the total width of the table and an average width of a character will return the width of each column. If something is already available then I do not want to replace the wheel again.

Certainly this can not be true when the font width is complete, but an approximation must be corrected only. Or maybe there is a configurable table with width for each character.

Any sign will be appreciated

It is not easy.

In PHPExcel, when a cell is set to Autowidth, we use the gd library's imagettfbbox () function

  // font size actually supplied in pixels in GD2 But since GD 2 72 takes the DPI, pixels and digits, the same $ fontFile = self :: getTrueTypeFontFileFromFont ($ font); $ Text box = spec pack ($ font-> getSize ($), $ rotation, $ fontfile, $ text); // corner position $ lower left convert x = $ text box [0]; $ LowerLeftCornerY = $ text box [1]; $ Critecorer X = $ text box [2]; $ LowerRightCornerY = $ text box [3]; $ Upper Right Corner X = $ Text Box [4]; $ Upper Right Wrong = $ Text Box [5]; $ Upper LeftConer X = $ Text Box [6]; $ Upper Leftcorner Y = $ Text Box [7]; // Consider the rotation when calculating width $ textWidth = maximum ($ Right Writer X - $ Upper Leftcorerux, $ Upper Right Cornex - $ Lower LeftConer); $ TextWidth return;  

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

  // pixel Calculate the column width in We consider fixed glyph width. The result varies with the font name and size. Switch ($ fontName) {Case 'Calibri': // value 8.26 calibrary 11 found through the interpolation by examining real Excel files with the font. $ Column wythath = (int) (8.26 * PHPExcel_Shared_String :: countersiater ($ column text)); $ ColumnWidth = $ columnWidth * $ fontSize / 11; // extrapolation by breaking font size; Case 'Ariel': // Price 7 ARL 10 with the font inspection of real excel files and found through interpolation. $ Column withth = (int) (7 * PHPExcel_Shared_String :: countakeharacters ($ column text)); $ Column width = $ columnWidth * $ fontSize / 10; // extrapolation by breaking font size; Case 'Verdana': // value 8 was found through interpolation by inspecting real Excel files with Word 10 10 fonts. $ Column wythth = (int) (8 * PHPExcel_Shared_String :: countakeharacters ($ column text)); $ Column width = $ columnWidth * $ fontSize / 10; // extrapolation by breaking font size; Default: // Simply Capture Calorie $ column = (int) (8.26 * PHPExcel_Shared_String :: CountKeers ($ column text)); $ ColumnWidth = $ columnWidth * $ fontSize / 11; // extrapolation by breaking font size; } // Calculate estimated rotated column width if ($ rotation! == 0) {if ($ rotation == -165) {// stacked text $ columnWidth = 4; // approximation} other {// curved text $ column width = $ columnWidth * cos + $ fontSize * abs (sin (deg2rad ($ rotation))) / 5; // approximation}} // pixel width is an integer $ column width = (int) $ columnWidth; $ ColumnWidth return;  

Comments

Popular posts from this blog

windows - Heroku throws SQLITE3 Read only exception -

lex - Building a lexical Analyzer in Java -

python - rename keys in a dictionary -