Skip to content

API Value Objects

Tous les value objects dans TCPDF-Next sont immuables. Chaque méthode "modificatrice" retourne une nouvelle instance, laissant l'original inchangé. Cela élimine une catégorie de bugs causés par l'état mutable partagé.


PageSize

Namespace: TcpdfNext\ValueObjects\PageSize

Représente les dimensions d'une page. En interne toutes les mesures sont stockées en points PDF (1/72 pouce).

Méthodes factory (Série A ISO)

PageSize::a0(): static
841 x 1189 mm
PageSize::a1(): static
594 x 841 mm
PageSize::a2(): static
420 x 594 mm
PageSize::a3(): static
297 x 420 mm
PageSize::a4(): static
210 x 297 mm (par défaut)
PageSize::a5(): static
148 x 210 mm
PageSize::a6(): static
105 x 148 mm

Méthodes factory (Série B ISO)

PageSize::b0(): static
1000 x 1414 mm
PageSize::b1(): static
707 x 1000 mm
PageSize::b2(): static
500 x 707 mm
PageSize::b3(): static
353 x 500 mm
PageSize::b4(): static
250 x 353 mm
PageSize::b5(): static
176 x 250 mm

Méthodes factory (Nord-américain)

PageSize::letter(): static
8.5 x 11 in (215.9 x 279.4 mm)
PageSize::legal(): static
8.5 x 14 in (215.9 x 355.6 mm)
PageSize::tabloid(): static
11 x 17 in (279.4 x 431.8 mm)

Personnalisé et recherche

PageSize::fromMm(float $width, float $height, string $name = 'Custom'): static
Créer une taille de page depuis dimensions en millimètres.
PageSize::fromPoints(float $width, float $height, string $name = 'Custom'): static
Créer une taille de page depuis dimensions en points.
PageSize::fromName(string $name): static
Rechercher une taille de page standard par nom (insensible à la casse). Lève InvalidArgumentException pour noms inconnus.

Helpers d'orientation

landscape(): static
Retourner un nouveau PageSize avec largeur et hauteur échangées pour que largeur > hauteur.
portrait(): static
Retourner un nouveau PageSize avec largeur et hauteur échangées pour que hauteur > largeur.

Propriétés

PropriétéTypeDescription
$widthfloatLargeur en points
$heightfloatHauteur en points
$namestringNom lisible (ex., 'A4', 'Letter')

Méthodes de commodité

widthMm(): float
Retourner la largeur en millimètres.
heightMm(): float
Retourner la hauteur en millimètres.

Exemple

php
use TcpdfNext\ValueObjects\PageSize;

$a4 = PageSize::a4();
$landscape = $a4->landscape();   // 297 x 210 mm
$custom = PageSize::fromMm(140, 216, 'Half Letter');
$letter = PageSize::fromName('Letter');

Margin

Namespace: TcpdfNext\ValueObjects\Margin

Représente les marges de page à quatre côtés. Stockage interne en points PDF.

Méthodes factory

Margin::mm(float $top, float $right, float $bottom, float $left): static
Créer des marges depuis valeurs en millimètres.
Margin::pt(float $top, float $right, float $bottom, float $left): static
Créer des marges depuis valeurs en points.
Margin::uniform(float $value): static
Les quatre côtés égaux (en points).
Margin::uniformMm(float $value): static
Les quatre côtés égaux (en millimètres).
Margin::symmetric(float $horizontal, float $vertical): static
Marges symétriques (horizontal = gauche + droite, vertical = haut + bas) en points.
Margin::zero(): static
Toutes les marges à zéro.

Propriétés

PropriétéTypeDescription
$topfloatMarge haute en points
$rightfloatMarge droite en points
$bottomfloatMarge basse en points
$leftfloatMarge gauche en points

Méthodes modificatrices

withTop(float $value): static
Retourner un nouveau Margin avec la valeur haute remplacée (en points).
withRight(float $value): static
Retourner un nouveau Margin avec la valeur droite remplacée.
withBottom(float $value): static
Retourner un nouveau Margin avec la valeur basse remplacée.
withLeft(float $value): static
Retourner un nouveau Margin avec la valeur gauche remplacée.

Méthodes de calcul

printableArea(PageSize $pageSize): Dimension
Retourner la Dimension imprimable (taille page moins marges).

Exemple

php
use TcpdfNext\ValueObjects\Margin;
use TcpdfNext\ValueObjects\PageSize;

$margin = Margin::mm(top: 15, right: 20, bottom: 15, left: 20);
$narrow = $margin->withLeft(36.0)->withRight(36.0);
$area = $margin->printableArea(PageSize::a4());
echo $area->widthMm(); // 170.0

Position

Namespace: TcpdfNext\ValueObjects\Position

Une paire de coordonnées x-y immuable en points PDF.

Méthodes factory

new Position(float $x, float $y)
Créer depuis valeurs en points.
Position::mm(float $x, float $y): static
Créer depuis valeurs en millimètres (converties en points en interne).

Propriétés

PropriétéTypeDescription
$xfloatCoordonnée horizontale en points
$yfloatCoordonnée verticale en points

Méthodes

translate(float $dx, float $dy): static
Retourner une nouvelle Position décalée de (dx, dy) points.
xMm(): float
Retourner x en millimètres.
yMm(): float
Retourner y en millimètres.

Exemple

php
use TcpdfNext\ValueObjects\Position;

$pos = Position::mm(x: 25.4, y: 50.8);
$shifted = $pos->translate(dx: 10, dy: 20);
echo $shifted->xMm(); // ~28.9

Dimension

Namespace: TcpdfNext\ValueObjects\Dimension

Une paire largeur-hauteur immuable en points PDF.

Méthodes factory

new Dimension(float $width, float $height)
Créer depuis valeurs en points.
Dimension::mm(float $width, float $height): static
Créer depuis valeurs en millimètres.

Propriétés

PropriétéTypeDescription
$widthfloatLargeur en points
$heightfloatHauteur en points

Méthodes

swap(): static
Retourner une nouvelle Dimension avec largeur et hauteur échangées.
widthMm(): float
Retourner la largeur en millimètres.
heightMm(): float
Retourner la hauteur en millimètres.
area(): float
Retourner l'aire en points carrés.

Exemple

php
use TcpdfNext\ValueObjects\Dimension;

$dim = Dimension::mm(width: 210, height: 297);
echo $dim->width;      // 595.28
echo $dim->widthMm();  // 210.0
$swapped = $dim->swap(); // équivalent 297 x 210 mm

Unit

Namespace: TcpdfNext\ValueObjects\Unit

Classe utilitaire statique pour convertir entre unités de mesure. Toutes les conversions sont basées sur le standard PDF de 72 points par pouce.

Méthodes de conversion

Unit::mm(float $value): float
Convertir millimètres en points. Alias pour mmToPoints().
Unit::pt(float $value): float
Identité -- retourne la valeur inchangée (pour cohérence API).
Unit::cm(float $value): float
Convertir centimètres en points.
Unit::in(float $value): float
Convertir pouces en points (valeur x 72).
Unit::mmToPoints(float $mm): float
Convertir millimètres en points (mm x 72 / 25.4).
Unit::pointsToMm(float $pt): float
Convertir points en millimètres (pt x 25.4 / 72).
Unit::inchesToPoints(float $inches): float
Convertir pouces en points (pouces x 72).
Unit::pointsToInches(float $pt): float
Convertir points en pouces (pt / 72).
Unit::cmToPoints(float $cm): float
Convertir centimètres en points (cm x 72 / 2.54).
Unit::pointsToCm(float $pt): float
Convertir points en centimètres (pt x 2.54 / 72).
Unit::convert(float $value, string $from, string $to): float
Convertisseur général. Chaînes unités supportées : 'mm', 'cm', 'in', 'pt'.

Exemple

php
use TcpdfNext\ValueObjects\Unit;

$points = Unit::mmToPoints(210.0);       // 595.28
$mm     = Unit::pointsToMm(595.28);      // 210.0
$points = Unit::inchesToPoints(8.5);     // 612.0
$result = Unit::convert(1.0, 'in', 'mm'); // 25.4

Color

Namespace: TcpdfNext\Graphics\Color

Représentation couleur immuable supportant plusieurs espaces colorimétriques. Bien que Color vive dans le package Graphics, elle est utilisée de manière omniprésente dans la bibliothèque (couleur texte, couleur dessin, couleur remplissage, couleur signet, couleur annotation).

Méthodes factory

Color::rgb(int $r, int $g, int $b): static
Créer une couleur DeviceRGB. Valeurs de 0 à 255.
Color::cmyk(float $c, float $m, float $y, float $k): static
Créer une couleur DeviceCMYK. Valeurs de 0 à 100.
Color::gray(int $level): static
Créer une couleur DeviceGray. Niveau de 0 (noir) à 255 (blanc).
Color::spot(string $name, Color $fallback, float $tint = 100): static
Créer une couleur spot nommée (Separation) avec fallback couleur processus et pourcentage de teinte.
Color::hex(string $hex): static
Créer une couleur depuis chaîne hex 3 ou 6 chiffres (avec ou sans '#').
Color::black(): static
Commodité : rgb(0, 0, 0).
Color::white(): static
Commodité : rgb(255, 255, 255).
Color::transparent(): static
Commodité : couleur entièrement transparente.

Propriétés

PropriétéTypeDescription
$spaceColorSpaceL'enum espace colorimétrique (DeviceRGB, DeviceCMYK, DeviceGray, Separation)

Méthodes

toRgbArray(): array
Retourner [r, g, b] avec valeurs 0-255. Convertit depuis CMYK ou Gray si nécessaire.
toCmykArray(): array
Retourner [c, m, y, k] avec valeurs 0-100.
toHex(): string
Retourner une chaîne hex 6 chiffres (ex., 'ff6600').
withAlpha(float $alpha): static
Retourner une nouvelle Color avec l'alpha donné (0.0-1.0).
equals(Color $other): bool
Comparer deux couleurs pour égalité de valeur.

Exemple

php
use TcpdfNext\Graphics\Color;

$brand  = Color::hex('#0066CC');
$print  = Color::cmyk(100, 0, 0, 0);
$spot   = Color::spot('PANTONE 286 C', Color::cmyk(100, 66, 0, 2));
$faded  = $brand->withAlpha(0.5);
echo $brand->toHex(); // '0066cc'

Voir aussi

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