Question

J'ai une grille de données que je veux exporter au format RTF, PDF, etc. en utilisant divers (et non parfaits) convertisseurs PHP / générateurs.

Qu'est-ce que je manque le plus est le réglage automatique de la table HTML de la largeur des colonnes en fonction des longueurs de chaînes dans les cellules (chaînes contiennent des sauts de ligne dont les choses compliquent un peu, car ils doivent être conservés).

je besoin d'un algorithme qui, étant donné le contenu des cellules (texte brut), une largeur totale de la table et une largeur moyenne d'un caractère, renverrait une largeur de chaque colonne. Je ne voudrais pas réinventer la roue si quelque chose est déjà disponible.

Bien sûr, il ne peut pas être parfait si la police est une largeur variable, mais une approximation serait très bien. Ou il pourrait peut-être avoir une table configurable avec des largeurs pour chaque caractère.

Tout indice serait apprécié.

Était-ce utile?

La solution

Ce n'est pas facile.

Dans PHPExcel, lorsqu'une cellule est réglée sur Largeur automatique, on utilise la fonction bibliothèque 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;

Il est bien que très intense, surtout lorsque l'on travaille avec de grandes feuilles de calcul, nous avons donc aussi une méthode alternative (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;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top