Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 以类似HTML的方式计算列宽(基于单元格内容)_Php_Algorithm_Html Table_Autosize_Column Width - Fatal编程技术网

Php 以类似HTML的方式计算列宽(基于单元格内容)

Php 以类似HTML的方式计算列宽(基于单元格内容),php,algorithm,html-table,autosize,column-width,Php,Algorithm,Html Table,Autosize,Column Width,我有一个数据网格,我想使用各种(但不是完美的)PHP转换器/生成器导出到RTF、PDF等 我最缺少的是HTML表格根据单元格中字符串的长度自动调整列宽(字符串包含换行符,这会使事情变得有点复杂,因为它们应该被保留) 我需要一个算法,给定单元格的内容(纯文本)、表的总宽度和字符的平均宽度,将返回每列的宽度。如果已经有东西可用,我不想重新发明轮子 当然,如果字体是可变宽度的,它就不可能是完美的,但是近似值就可以了。或者它可能有一个可配置的表格,每个字符都有宽度 任何暗示都将不胜感激 这不是一件容易的

我有一个数据网格,我想使用各种(但不是完美的)PHP转换器/生成器导出到RTF、PDF等

我最缺少的是HTML表格根据单元格中字符串的长度自动调整列宽(字符串包含换行符,这会使事情变得有点复杂,因为它们应该被保留)

我需要一个算法,给定单元格的内容(纯文本)、表的总宽度和字符的平均宽度,将返回每列的宽度。如果已经有东西可用,我不想重新发明轮子

当然,如果字体是可变宽度的,它就不可能是完美的,但是近似值就可以了。或者它可能有一个可配置的表格,每个字符都有宽度


任何暗示都将不胜感激

这不是一件容易的事

在PHPExcel中,当单元格设置为autowidth时,我们使用gd library imagettfbbox()函数

但是它非常密集,特别是在处理大型工作表时,因此我们还有一种替代(近似)方法


你好我实际上使用的是PHPExcel(我很喜欢),但这不是我的问题,它可以很好地处理自动调整列大小(除了使用富文本连接的单元格)。我将从您的代码开始,并可能更新它以处理换行符(CountCharacters函数)和一些用于包装文本的启发式方法,以保持列的总宽度小于页面的宽度(电子表格通常没有此约束)。非常感谢。
// 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;
// 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;