テキストフォーマット
テキストフォーマットの全範囲を実演します:フォントファミリ、ボールド/イタリックスタイル、ポイントサイズ、RGB色、Alignment enumによる配置、複数行の段落。
完全なサンプル
php
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use TcpdfNext\Document;
use TcpdfNext\Enums\Alignment;
Document::create()
->setTitle('Text Formatting')
->addPage()
// -- フォントファミリ ---------------------------------------------------
->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)
// -- スタイル ----------------------------------------------------------
->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)
// -- サイズ -----------------------------------------------------------
->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)
// -- 色 ----------------------------------------------------------
->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)
// -- 配置 -------------------------------------------------------
->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)
// -- 複数行の段落 --------------------------------------------
->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;重要なコンセプト
スタイル文字
| 文字 | 意味 |
|---|---|
| (空) | レギュラー |
B | ボールド |
I | イタリック |
BI | ボールド + イタリック |
setTextColor(r, g, b)
0--255のRGB整数を受け取ります。テキスト書き込みメソッドの前に呼び出してください:
php
->setTextColor(13, 110, 253) // 青Alignment Enum
| 値 | 効果 |
|---|---|
Alignment::Left | 左揃え(デフォルト) |
Alignment::Center | 中央揃え |
Alignment::Right | 右揃え |
Alignment::Justified | 両端揃え(multiCellのみ) |
cell() vs multiCell()
cell()-- 単一行、折り返しなし。multiCell()-- 指定幅内でテキストを折り返し、両端揃えをサポート。
出力
生成されるPDFには、3つのフォントファミリ、4つのスタイルバリアント、4つのサイズ、3つの色、3つの配置モード、両端揃えの段落を表示する1ページが含まれます。