Skip to content

テキスト出力(HasTextOutput)

HasTextOutput トレイトは、ページ上にテキストを配置するための主要なメソッドを提供します。各メソッドは異なるレイアウトモデルに対応しています:固定セル、フローテキスト、絶対位置指定、HTMLレンダリング。

クイックリファレンス

メソッドレイアウトモデル折り返し
cell()単一行ボックスなし — テキストはクリップされます
multiCell()複数行ブロックあり — セル幅で自動折り返し
text()絶対位置なし
write()インラインフローあり — ワードプロセッサのように流れます
writeHtml()HTMLブロックあり — 完全なHTMLレイアウト
writeHtmlCell()位置指定セル内のHTMLあり
ln()改行該当なし

基本例

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Single line cell', newLine: true)
    ->multiCell(0, 10, 'This is a longer text that will automatically wrap to multiple lines when it reaches the edge of the printable area.')
    ->ln(5)
    ->text(50.0, 100.0, 'Absolute positioned text')
    ->write(10, 'Inline text that ')
    ->write(10, 'continues flowing.');

すべてのメソッドは static を返すため、すべての呼び出しをチェーンできます。

cell()

オプションの罫線、背景塗りつぶし、配置、リンクを持つ単一行の矩形セルを描画します。

php
$pdf->cell(
    float  $w,           // 幅(0 = 右余白まで拡張)
    float  $h,           // 高さ
    string $txt,         // テキスト内容
    mixed  $border = 0,  // 0、1、または 'LTRB' の組み合わせ
    bool   $newLine = false,
    string $align  = '',  // L, C, R, J
    bool   $fill   = false,
    mixed  $link   = '',
);

カーソルはセルの右側に移動します($newLinetrue の場合は次の行に移動します)。

配置

php
use Yeeefang\TcpdfNext\Contracts\Enums\Alignment;

$pdf->cell(0, 10, 'Left aligned')
    ->ln()
    ->cell(0, 10, 'Centered', align: 'C')
    ->ln()
    ->cell(0, 10, 'Right aligned', align: 'R')
    ->ln()
    ->cell(0, 10, 'Justified text in a cell', align: 'J');

罫線

php
// 罫線なし
$pdf->cell(60, 10, 'No border', border: 0);

// 全枠
$pdf->cell(60, 10, 'Full frame', border: 1);

// 個別の辺 — Left, Top, Right, Bottom
$pdf->cell(60, 10, 'Top and bottom', border: 'TB');
$pdf->cell(60, 10, 'Left only', border: 'L');

塗りつぶし

php
$pdf->setFillColor(230, 230, 250)
    ->cell(0, 10, 'Lavender background', fill: true, newLine: true);

multiCell()

指定された幅で自動折り返しする複数行テキストブロックをレンダリングします。出力後、カーソルはセルの下に移動します。

php
$pdf->multiCell(
    float  $w,           // 幅(0 = 右余白まで拡張)
    float  $h,           // 最小行の高さ
    string $txt,         // テキスト内容
    mixed  $border = 0,
    string $align  = 'J',
    bool   $fill   = false,
);
php
$pdf->setFont('Helvetica', '', 11)
    ->multiCell(80, 6, 'This paragraph will wrap at 80mm width. The text flows naturally, respecting word boundaries and hyphenation rules.', border: 1, align: 'J');

text()

絶対位置(x, y)に単一の文字列を配置します。カーソルはその後移動しません — これは「一方通行」のメソッドです。

php
$pdf->text(20.0, 50.0, 'Positioned at x=20, y=50');

正確な位置を制御するウォーターマーク、ラベル、オーバーレイコンテンツに text() を使用します。

write()

現在のカーソル位置にインラインでテキストを書き込みます。テキストは右余白に達すると自動的に折り返されます。ワードプロセッサでのタイピングと同様です。

php
$pdf->setFont('Helvetica', '', 12)
    ->write(6, 'This sentence starts here and ')
    ->setFont('Helvetica', 'B', 12)
    ->write(6, 'this part is bold')
    ->setFont('Helvetica', '', 12)
    ->write(6, ' then back to normal.');

最初のパラメータ($h)は行の高さです。文中でフォントやスタイルを変更する必要がある場合は write() を使用します。

writeHtml()

組み込みHTMLパーサーを使用してHTML文字列をレンダリングします。見出し、段落、テーブル、リスト、インラインスタイルなど一般的なタグをサポートしています。

php
$pdf->writeHtml('<h2>Section Title</h2><p>Paragraph with <b>bold</b> and <i>italic</i> text.</p>');

テーブル

php
$html = '
<table border="1" cellpadding="4">
    <thead>
        <tr>
            <th>Product</th>
            <th>Qty</th>
            <th>Price</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Widget A</td>
            <td align="center">10</td>
            <td align="right">$25.00</td>
        </tr>
        <tr>
            <td>Widget B</td>
            <td align="center">5</td>
            <td align="right">$42.50</td>
        </tr>
    </tbody>
</table>';

$pdf->writeHtml($html);

writeHtmlCell()

HTMLレンダリングとセル位置指定を組み合わせます。HTMLコンテンツは指定された座標のセル内に配置されます。

php
$pdf->writeHtmlCell(
    float  $w,     // 幅
    float  $h,     // 最小高さ
    float  $x,     // X位置
    float  $y,     // Y位置
    string $html,
    mixed  $border = 0,
    bool   $fill   = false,
);
php
$pdf->writeHtmlCell(90, 0, 10, 50, '<p style="color:#336699;">Positioned HTML content with <b>formatting</b>.</p>', border: 1);

ln()

改行を挿入します。カーソルは左余白に移動し、指定された高さ分だけ下に移動します。

php
$pdf->ln();       // 最後のセルの高さを使用した改行
$pdf->ln(10);     // 10mmの垂直間隔での改行
$pdf->ln(0);      // 垂直移動なしで左余白に移動

適切なメソッドの選択

シナリオメソッド
テーブルセル、ラベル、単一行データcell()
段落、説明文、長いテキストmultiCell()
ウォーターマーク、スタンプ、絶対位置ラベルtext()
混合フォーマットのインラインテキストwrite()
HTMLマークアップを含むリッチコンテンツwriteHtml()
特定位置のHTMLコンテンツwriteHtmlCell()

LGPL-3.0-or-later ライセンスの下で公開されています。