Skip to content

Formatage de texte

Démontre la gamme complète de formatage de texte : familles de polices, styles gras/italique, tailles en points, couleurs RGB, alignement via l'enum Alignment et paragraphes multi-lignes.

Exemple complet

php
<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use TcpdfNext\Document;
use TcpdfNext\Enums\Alignment;

Document::create()
    ->setTitle('Text Formatting')
    ->addPage()

    // -- Familles de polices ---------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->setTextColor(33, 37, 41)
    ->cell(0, 12, 'Font Families', newLine: true)

    ->setFont('helvetica', size: 12)
    ->cell(0, 8, 'Helvetica -- clean sans-serif', newLine: true)

    ->setFont('times', size: 12)
    ->cell(0, 8, 'Times -- classic serif', newLine: true)

    ->setFont('courier', size: 12)
    ->cell(0, 8, 'Courier -- monospace', newLine: true)

    // -- Styles ----------------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->cell(0, 16, 'Font Styles', newLine: true)

    ->setFont('helvetica', size: 12)
    ->cell(0, 8, 'Regular text', newLine: true)

    ->setFont('helvetica', style: 'B', size: 12)
    ->cell(0, 8, 'Bold text', newLine: true)

    ->setFont('helvetica', style: 'I', size: 12)
    ->cell(0, 8, 'Italic text', newLine: true)

    ->setFont('helvetica', style: 'BI', size: 12)
    ->cell(0, 8, 'Bold + Italic text', newLine: true)

    // -- Tailles -----------------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->cell(0, 16, 'Font Sizes', newLine: true)

    ->setFont('helvetica', size: 8)
    ->cell(0, 6, '8 pt -- fine print', newLine: true)

    ->setFont('helvetica', size: 12)
    ->cell(0, 8, '12 pt -- body text', newLine: true)

    ->setFont('helvetica', size: 20)
    ->cell(0, 12, '20 pt -- subheading', newLine: true)

    ->setFontSize(28)
    ->cell(0, 16, '28 pt -- heading', newLine: true)

    // -- Couleurs ----------------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->setTextColor(33, 37, 41)
    ->cell(0, 16, 'Text Colors', newLine: true)

    ->setFont('helvetica', size: 12)
    ->setTextColor(220, 53, 69)
    ->cell(0, 8, 'Red (220, 53, 69)', newLine: true)

    ->setTextColor(25, 135, 84)
    ->cell(0, 8, 'Green (25, 135, 84)', newLine: true)

    ->setTextColor(13, 110, 253)
    ->cell(0, 8, 'Blue (13, 110, 253)', newLine: true)

    ->setTextColor(33, 37, 41)

    // -- Alignement -------------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->cell(0, 16, 'Text Alignment', newLine: true)

    ->setFont('helvetica', size: 12)
    ->cell(0, 8, 'Left-aligned (default)', align: Alignment::Left, newLine: true)
    ->cell(0, 8, 'Center-aligned',         align: Alignment::Center, newLine: true)
    ->cell(0, 8, 'Right-aligned',          align: Alignment::Right, newLine: true)

    // -- Paragraphe multi-lignes --------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->cell(0, 16, 'Multi-Line Text', newLine: true)

    ->setFont('helvetica', size: 11)
    ->multiCell(
        width:  0,
        height: 7,
        text:   "TCPDF-Next wraps text automatically with multiCell().\n"
              . "Use the Alignment enum for justified, left, center, or right alignment.\n"
              . "Line spacing is controlled by the height parameter.",
        align:  Alignment::Justified,
    )

    ->save(__DIR__ . '/text-formatting.pdf');

echo 'PDF created.' . PHP_EOL;

Concepts clés

Caractères de style

CaractèreSignification
(vide)Normal
BGras
IItalique
BIGras + Italique

setTextColor(r, g, b)

Accepte des entiers RGB 0--255. Appelez-la avant toute méthode d'écriture de texte :

php
->setTextColor(13, 110, 253)   // bleu

Enum Alignment

ValeurEffet
Alignment::LeftAligné à gauche (par défaut)
Alignment::CenterCentré
Alignment::RightAligné à droite
Alignment::JustifiedJustifié (multiCell uniquement)

cell() vs multiCell()

  • cell() -- une ligne, pas de retour à la ligne.
  • multiCell() -- retour à la ligne du texte dans la largeur donnée et supporte la justification.

Sortie

Le PDF généré contient une page présentant trois familles de polices, quatre variantes de style, quatre tailles, trois couleurs, trois modes d'alignement et un paragraphe justifié.

Distribué sous licence LGPL-3.0-or-later.