値オブジェクトAPI
TCPDF-Nextのすべての値オブジェクトは不変です。すべての「変更」メソッドは新しいインスタンスを返し、元のインスタンスは変更されません。これにより、共有された可変状態に起因するバグのカテゴリが排除されます。
PageSize
名前空間: TcpdfNext\ValueObjects\PageSize
ページの寸法を表します。内部的にすべての測定値はPDFポイント(1/72インチ)で格納されます。
ファクトリメソッド(ISO Aシリーズ)
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(デフォルト)
PageSize::a5(): static
148 x 210 mm
PageSize::a6(): static
105 x 148 mm
ファクトリメソッド(ISO Bシリーズ)
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
ファクトリメソッド(北米)
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)
カスタムと検索
PageSize::fromMm(float $width, float $height, string $name = 'Custom'): static
ミリメートル寸法からページサイズを作成します。
PageSize::fromPoints(float $width, float $height, string $name = 'Custom'): static
ポイント寸法からページサイズを作成します。
PageSize::fromName(string $name): static
名前で標準ページサイズを検索します(大文字小文字区別なし)。不明な名前の場合はInvalidArgumentExceptionをスローします。
向きヘルパー
landscape(): static
幅 > 高さになるように幅と高さを入れ替えた新しいPageSizeを返します。
portrait(): static
高さ > 幅になるように幅と高さを入れ替えた新しいPageSizeを返します。
プロパティ
| プロパティ | 型 | 説明 |
|---|---|---|
$width | float | ポイント単位の幅 |
$height | float | ポイント単位の高さ |
$name | string | 人間が読める名前(例:'A4'、'Letter') |
便利メソッド
widthMm(): float
ミリメートル単位の幅を返します。
heightMm(): float
ミリメートル単位の高さを返します。
サンプル
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
名前空間: TcpdfNext\ValueObjects\Margin
四辺のページマージンを表します。内部ストレージはPDFポイントです。
ファクトリメソッド
Margin::mm(float $top, float $right, float $bottom, float $left): static
ミリメートル値からマージンを作成します。
Margin::pt(float $top, float $right, float $bottom, float $left): static
ポイント値からマージンを作成します。
Margin::uniform(float $value): static
4辺すべて同じ(ポイント単位)。
Margin::uniformMm(float $value): static
4辺すべて同じ(ミリメートル単位)。
Margin::symmetric(float $horizontal, float $vertical): static
対称マージン(horizontal = 左 + 右、vertical = 上 + 下)ポイント単位。
Margin::zero(): static
すべてのマージンをゼロに設定します。
プロパティ
| プロパティ | 型 | 説明 |
|---|---|---|
$top | float | ポイント単位の上マージン |
$right | float | ポイント単位の右マージン |
$bottom | float | ポイント単位の下マージン |
$left | float | ポイント単位の左マージン |
変更メソッド
withTop(float $value): static
上の値を置き換えた新しいMarginを返します(ポイント単位)。
withRight(float $value): static
右の値を置き換えた新しいMarginを返します。
withBottom(float $value): static
下の値を置き換えた新しいMarginを返します。
withLeft(float $value): static
左の値を置き換えた新しいMarginを返します。
計算メソッド
printableArea(PageSize $pageSize): Dimension
印刷可能なDimension(ページサイズからマージンを引いたもの)を返します。
サンプル
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.0Position
名前空間: TcpdfNext\ValueObjects\Position
PDFポイントでの不変のx--y座標ペア。
ファクトリメソッド
new Position(float $x, float $y)
ポイント値から作成します。
Position::mm(float $x, float $y): static
ミリメートル値から作成します(内部的にポイントに変換されます)。
プロパティ
| プロパティ | 型 | 説明 |
|---|---|---|
$x | float | ポイント単位の水平座標 |
$y | float | ポイント単位の垂直座標 |
メソッド
translate(float $dx, float $dy): static
(dx, dy)ポイント分シフトした新しいPositionを返します。
xMm(): float
ミリメートル単位のxを返します。
yMm(): float
ミリメートル単位のyを返します。
サンプル
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.9Dimension
名前空間: TcpdfNext\ValueObjects\Dimension
PDFポイントでの不変の幅--高さペア。
ファクトリメソッド
new Dimension(float $width, float $height)
ポイント値から作成します。
Dimension::mm(float $width, float $height): static
ミリメートル値から作成します。
プロパティ
| プロパティ | 型 | 説明 |
|---|---|---|
$width | float | ポイント単位の幅 |
$height | float | ポイント単位の高さ |
メソッド
swap(): static
幅と高さを入れ替えた新しいDimensionを返します。
widthMm(): float
ミリメートル単位の幅を返します。
heightMm(): float
ミリメートル単位の高さを返します。
area(): float
平方ポイント単位の面積を返します。
サンプル
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(); // 297 x 210 mm相当Unit
名前空間: TcpdfNext\ValueObjects\Unit
測定単位間の変換を行う静的ユーティリティクラスです。すべての変換は、1インチあたり72ポイントのPDF標準に基づいています。
変換メソッド
Unit::mm(float $value): float
ミリメートルをポイントに変換します。mmToPoints()のエイリアスです。
Unit::pt(float $value): float
恒等式 -- 値をそのまま返します(APIの一貫性のため)。
Unit::cm(float $value): float
センチメートルをポイントに変換します。
Unit::in(float $value): float
インチをポイントに変換します(値 x 72)。
Unit::mmToPoints(float $mm): float
ミリメートルをポイントに変換します(mm x 72 / 25.4)。
Unit::pointsToMm(float $pt): float
ポイントをミリメートルに変換します(pt x 25.4 / 72)。
Unit::inchesToPoints(float $inches): float
インチをポイントに変換します(inches x 72)。
Unit::pointsToInches(float $pt): float
ポイントをインチに変換します(pt / 72)。
Unit::cmToPoints(float $cm): float
センチメートルをポイントに変換します(cm x 72 / 2.54)。
Unit::pointsToCm(float $pt): float
ポイントをセンチメートルに変換します(pt x 2.54 / 72)。
Unit::convert(float $value, string $from, string $to): float
汎用コンバーター。サポートされる単位文字列:'mm'、'cm'、'in'、'pt'。
サンプル
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.4Color
名前空間: TcpdfNext\Graphics\Color
複数のカラースペースをサポートする不変のカラー表現です。ColorはGraphicsパッケージに属しますが、ライブラリ全体で広く使用されています(テキスト色、描画色、塗りつぶし色、ブックマーク色、注釈色)。
ファクトリメソッド
Color::rgb(int $r, int $g, int $b): static
DeviceRGBカラーを作成します。値の範囲は0〜255です。
Color::cmyk(float $c, float $m, float $y, float $k): static
DeviceCMYKカラーを作成します。値の範囲は0〜100です。
Color::gray(int $level): static
DeviceGrayカラーを作成します。レベルの範囲は0(黒)〜255(白)です。
Color::spot(string $name, Color $fallback, float $tint = 100): static
プロセスカラーフォールバックとティントパーセンテージを持つ名前付きスポット(Separation)カラーを作成します。
Color::hex(string $hex): static
3桁または6桁の16進文字列('#'付きまたはなし)からカラーを作成します。
Color::black(): static
便利メソッド:rgb(0, 0, 0)。
Color::white(): static
便利メソッド:rgb(255, 255, 255)。
Color::transparent(): static
便利メソッド:完全透明カラー。
プロパティ
| プロパティ | 型 | 説明 |
|---|---|---|
$space | ColorSpace | カラースペースEnum(DeviceRGB、DeviceCMYK、DeviceGray、Separation) |
メソッド
toRgbArray(): array
値0--255の[r, g, b]を返します。必要に応じてCMYKまたはGrayから変換します。
toCmykArray(): array
値0--100の[c, m, y, k]を返します。
toHex(): string
6桁の16進文字列を返します(例:'ff6600')。
withAlpha(float $alpha): static
指定されたアルファ値(0.0--1.0)を持つ新しいColorを返します。
equals(Color $other): bool
2つのカラーを値の等価性で比較します。
サンプル
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'関連項目
- API概要 -- すべてのパッケージを一覧で確認
- Document API -- これらの値オブジェクトを受け取るメソッド
- 列挙型リファレンス -- ColorSpace、Orientationなど関連する列挙型
- 値オブジェクトガイド -- 概念的な概要と高度なパターン