حساب الأعمدة في مثل HTML بطريقة (استنادا إلى محتويات الخلية)

StackOverflow https://stackoverflow.com/questions/3051809

سؤال

لدي شبكة من البيانات التي تريد تصدير إلى RTF, PDF.... الخباستخدام مختلف (وليس الكمال) PHP المحولات/مولدات.

ما أنا في عداد المفقودين هو HTML الجدول التلقائي تعديل عرض الأعمدة على أساس أطوال السلاسل في الخلايا (السلاسل التي تحتوي على فواصل الأسطر التي يعقد الأمور قليلا ، كما ينبغي الحفاظ عليها).

أحتاج خوارزمية أنه بالنظر إلى محتويات الخلايا (نص عادي), إجمالي عرض الجدول ومتوسط عرضها حرف ستعود عرض لكل عمود.أنا لا أريد أن اختراع العجلة إذا كان هناك شيء هو متاح بالفعل.

وبطبيعة الحال فإنه لا يمكن أن تكون مثالية إذا كان الخط هو عرض متغير ، ولكن تقريبي سوف تفعل ما يرام.أو ربما يمكن أن يكون شكلي الجدول مع الاعراض عن كل حرف.

أي تلميح سيكون موضع تقدير.

هل كانت مفيدة؟

المحلول

هذا ليس سهلا.

في PHPExcel عندما الخليوي هو autowidth ، ونحن نستخدم مكتبة gd 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;

هو مكثفة جدا على الرغم من ، خاصة عند العمل مع أوراق عمل كبيرة ، لذلك لدينا أيضا بديل (التقريب) طريقة

// 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;
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top