Frage

Ich habe ein Raster von Daten, dass ich auf den Export in RTF wollen, PDF usw. mit verschiedenen (und nicht perfekt) PHP-Wandler / Generatoren.

Was mir fehlt die meisten ist die HTML-Tabelle automatische Anpassung der Spaltenbreite basierend auf den Längen von Strings in den Zellen (Strings enthalten Zeilenumbrüche, die komplizieren die Dinge ein wenig, wie sie erhalten werden sollte).

ich brauche einen Algorithmus, der angesichts der Inhalte der Zellen (Klartext), eine Gesamtbreite des Tisches und einer durchschnittlichen Breite eines Zeichens, würde für jede Spalte eine Breite zurückzukehren. Ich mag nicht das Rad neu erfinden, wenn etwas bereits verfügbar ist.

Natürlich kann es nicht perfekt sein, wenn die Schriftart mit variabler Breite, aber eine Annäherung würde nur gut tun. Oder vielleicht könnte es eine konfigurierbare Tabelle für jedes Zeichen mit Breiten hat.

Jede mögliche Andeutung geschätzt.

War es hilfreich?

Lösung

Das ist nicht leicht.

In PHPExcel, wenn eine Zelle gesetzt ist AUTOBREITE, verwenden wir die gd Bibliothek imagettfbbox () Funktion

// 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;

Es ist sehr intensiv aber, besonders beim Arbeiten mit großen Arbeitsblatt arbeiten, so dass wir auch eine Alternative (Näherung) Methode haben

// 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;
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top