Skip to content

Value Objects

TCPDF-Next uses immutable value objects for all geometric and chromatic data. Every value object is declared final readonly — once created, it cannot be modified. Create new instances instead of mutating existing ones.

PageSize

Represents the dimensions of a page in points (1 pt = 1/72 inch).

php
use Yeeefang\TcpdfNext\ValueObjects\PageSize;

// ISO 216 A series
$a4 = PageSize::A4();             // 595.28 x 841.89 pt

// Orientation switching (returns a new instance)
$landscape = PageSize::A4()->landscape();   // 841.89 x 595.28 pt
$portrait = PageSize::A4()->portrait();     // 595.28 x 841.89 pt

// From string name
$letter = PageSize::fromName('Letter');

// Custom size in points
$custom = new PageSize(400.0, 600.0, 'custom');

Factory Methods

SeriesMethods
ISO AA0(), A1(), A2(), A3(), A4(), A5(), A6()
ISO BB0(), B1(), B2(), B3(), B4(), B5()
North AmericanLetter(), Legal(), Tabloid()

Margin

Defines the page margins in millimeters.

php
use Yeeefang\TcpdfNext\ValueObjects\Margin;

// Same value on all four sides
$uniform = Margin::uniform(10.0);               // 10mm all sides

// Symmetric: vertical and horizontal
$symmetric = Margin::symmetric(15.0, 10.0);     // 15mm top/bottom, 10mm left/right

// No margins
$zero = Margin::zero();

// Explicit: top, right, bottom, left
$explicit = new Margin(20.0, 15.0, 25.0, 15.0);

Access individual sides via public readonly properties: $margin->top, $margin->right, $margin->bottom, $margin->left.

Position and Dimension

Simple coordinate and size pairs with public readonly properties.

php
use Yeeefang\TcpdfNext\ValueObjects\{Position, Dimension};

$pos = new Position(50.0, 100.0);   // $pos->x, $pos->y
$dim = new Dimension(210.0, 297.0); // $dim->width, $dim->height

Unit

Handles unit definitions and conversions between points, millimeters, centimeters, and inches.

php
use Yeeefang\TcpdfNext\ValueObjects\Unit;

$mm = Unit::mm();  // Also: Unit::pt(), Unit::cm(), Unit::in()

// Convert between units
$points = Unit::convert(25.4, Unit::mm(), Unit::pt());   // 25.4mm = 72pt
$inches = Unit::convert(72.0, Unit::pt(), Unit::in());   // 72pt = 1in

Conversion Reference

FromTo Points
1 pt1.0
1 mm2.8346
1 cm28.3465
1 in72.0

Color

The Color class lives in the Graphics namespace and supports RGB, CMYK, grayscale, and spot color models. Like all value objects, it is immutable.

php
use Yeeefang\TcpdfNext\Graphics\Color;

// RGB (0-255)
$red = Color::rgb(255, 0, 0);
$orange = Color::rgb(255, 102, 0);

// CMYK (0-100)
$processBlack = Color::cmyk(0, 0, 0, 100);
$cyan = Color::cmyk(100, 0, 0, 0);

// Grayscale (0-255, where 0 = black, 255 = white)
$gray = Color::gray(128);

// Spot color (name + CMYK fallback)
$pantone = Color::spot('Pantone 151 C', 0, 60, 100, 0);

Putting It All Together

php
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\ValueObjects\{PageSize, Margin, Unit};
use Yeeefang\TcpdfNext\Graphics\Color;

$landscape = PageSize::A4()->landscape();
$margins = Margin::symmetric(15.0, 10.0);
$orange = Color::rgb(255, 102, 0);

$pdf = Document::create()
    ->addPage($landscape, margin: $margins)
    ->setFont('Helvetica', '', 12)
    ->setTextColor($orange)
    ->cell(0, 10, 'Styled with value objects')
    ->save('styled.pdf');

All value objects are final readonly. Methods like landscape() and portrait() return new instances -- they never mutate the original. This makes value objects safe to share across pages, threads, and function boundaries without defensive copying.

Released under the LGPL-3.0-or-later License.