基本的な使い方
このページでは、TCPDF-Next ドキュメントの基本的な構成要素を順に説明します:ドキュメントの作成、ページの追加、テキストの記述、画像の埋め込み、そして出力の生成です。
ドキュメントの作成
Document::create() 静的ファクトリは、すべての PDF の単一エントリポイントです:
use Yeeefang\TcpdfNext\Core\Document;
$doc = Document::create();すべてのパラメータはオプションです。デフォルト設定では、ミリメートル単位の A4 縦向きドキュメントが生成されます。オプションの完全なリストについては設定ページを参照してください。
ページの追加
ドキュメントは空の状態から始まります。コンテンツを記述する前に、少なくとも 1 回は addPage() を呼び出してください:
use Yeeefang\TcpdfNext\Core\Enums\PageSize;
use Yeeefang\TcpdfNext\Core\Enums\Orientation;
use Yeeefang\TcpdfNext\Core\ValueObjects\Margin;
// ドキュメントのデフォルトでページを追加
$doc->addPage();
// カスタムマージンの横向き Letter ページを追加
$doc->addPage(
pageSize: PageSize::Letter,
orientation: Orientation::Landscape,
margin: new Margin(left: 10, top: 10, right: 10, bottom: 10),
);利用可能なページサイズ
PageSize は、すべての標準 ISO および北米サイズを含む Backed Enum です:
| Enum 値 | サイズ |
|---|---|
PageSize::A3 | 297 × 420 mm |
PageSize::A4 | 210 × 297 mm |
PageSize::A5 | 148 × 210 mm |
PageSize::Letter | 215.9 × 279.4 mm |
PageSize::Legal | 215.9 × 355.6 mm |
カスタムサイズは PageSize::custom(width, height) でサポートされています。
フォントの設定
TCPDF-Next は、標準的な PDF ベースフォントに加え、Unicode 対応の DejaVu Sans ファミリーを同梱しています。
// 組み込みベースフォント
$doc->setFont('Helvetica', size: 12);
// 組み込み Unicode フォント
$doc->setFont('DejaVuSans', size: 10);
// ボールド / イタリックバリアント
$doc->setFont('Helvetica', style: FontStyle::Bold, size: 14);
$doc->setFont('Helvetica', style: FontStyle::BoldItalic, size: 14);カスタムフォント
TrueType または OpenType フォントを登録し、エイリアスで使用します:
use Yeeefang\TcpdfNext\Core\Config\FontConfig;
$doc->configureFonts(function (FontConfig $config): void {
$config->addFont('/fonts/Inter-Regular.ttf', alias: 'Inter');
});
$doc->setFont('Inter', size: 11);テキスト出力
TCPDF-Next はページにテキストを配置するための 4 つのメソッドを提供しています。それぞれ異なるレイアウトのニーズに対応します。
cell()
単一行のセルを出力します。ラベル、テーブルセル、短いテキストに最適です:
$doc->cell(
width: 80,
height: 10,
text: 'Invoice #1042',
border: true,
align: Align::Center,
);multiCell()
指定した幅内で自動的に折り返されるテキストを出力します。呼び出しごとにカーソルが下に移動します:
$doc->multiCell(
width: 0, // 0 = 利用可能な全幅
height: 7,
text: 'This is a longer paragraph that will wrap across multiple lines '
. 'based on the available width and the current font size.',
);text()
絶対座標 (x, y) にテキストを配置します。カーソルは移動しません:
$doc->text(x: 105, y: 20, text: 'Centered Title', align: Align::Center);write()
現在のカーソル位置にインラインテキストを記述します。リンクをサポートし、段落内で自然に流れます:
$doc->write(height: 5, text: 'Visit the ');
$doc->write(height: 5, text: 'TCPDF-Next docs', link: 'https://tcpdf-next.dev');
$doc->write(height: 5, text: ' for more information.');画像
ファイルパスから
$doc->imageFromFile(
path: '/images/logo.png',
x: 15,
y: 15,
width: 40,
);文字列またはリソースから
$binary = file_get_contents('https://example.com/photo.jpg');
$doc->image(
data: $binary,
x: 15,
y: 60,
width: 50,
type: 'JPEG',
);サポートされるフォーマット: PNG、JPEG、GIF、SVG、WebP。
保存と出力
TCPDF-Next は、最終的な PDF を取得するためのいくつかの方法を提供しています。
ディスクに保存
$doc->save('/reports/invoice-1042.pdf');ブラウザに送信
use Yeeefang\TcpdfNext\Core\Enums\OutputDestination;
// インライン表示 (Content-Disposition: inline)
$doc->output('invoice.pdf', OutputDestination::Inline);
// 強制ダウンロード (Content-Disposition: attachment)
$doc->output('invoice.pdf', OutputDestination::Download);生の PDF データを取得
$pdfBytes = $doc->getPdfData();
// PSR-7 レスポンス、キュージョブ、S3 アップロードなどで使用OutputDestination Enum
| 値 | 動作 |
|---|---|
OutputDestination::Inline | インライン表示のためにブラウザに送信 |
OutputDestination::Download | ファイルダウンロードとしてブラウザに送信 |
OutputDestination::File | ファイルパスに書き出し(save() で内部使用) |
OutputDestination::String | 生のバイナリ文字列を返却(getPdfData() で内部使用) |
フルエントAPI
ほとんどのセッターは $this を返すため、チェーンによるフルエントスタイルが可能です:
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\Core\Enums\PageSize;
use Yeeefang\TcpdfNext\Core\Enums\Orientation;
use Yeeefang\TcpdfNext\Core\Enums\Align;
use Yeeefang\TcpdfNext\Core\Enums\FontStyle;
$pdf = Document::create()
->setTitle('Monthly Report')
->setAuthor('Acme Corp')
->addPage(pageSize: PageSize::A4, orientation: Orientation::Portrait)
->setFont('Helvetica', style: FontStyle::Bold, size: 18)
->cell(width: 0, height: 15, text: 'Monthly Report — February 2026', align: Align::Center)
->ln()
->setFont('Helvetica', size: 11)
->multiCell(width: 0, height: 6, text: 'This report summarises key metrics...')
->save('/reports/monthly.pdf');完全な例
すべてをまとめた例です:
use Yeeefang\TcpdfNext\Core\Document;
use Yeeefang\TcpdfNext\Core\Enums\Align;
use Yeeefang\TcpdfNext\Core\Enums\FontStyle;
use Yeeefang\TcpdfNext\Core\Enums\OutputDestination;
$doc = Document::create();
$doc->setTitle('Hello World');
$doc->setAuthor('TCPDF-Next');
$doc->addPage();
// ヘッダー
$doc->setFont('Helvetica', style: FontStyle::Bold, size: 20);
$doc->cell(width: 0, height: 15, text: 'Hello, TCPDF-Next!', align: Align::Center);
$doc->ln(20);
// 本文
$doc->setFont('DejaVuSans', size: 12);
$doc->multiCell(
width: 0,
height: 7,
text: 'TCPDF-Next is a modern, type-safe PDF generation library for PHP 8.5+. '
. 'It provides a clean API, strict static analysis, and comprehensive Unicode support.',
);
// ロゴ
$doc->imageFromFile(path: __DIR__ . '/logo.png', x: 15, y: 80, width: 30);
// 出力
$doc->output('hello.pdf', OutputDestination::Download);