Skip to content

基本的な使い方

このページでは、TCPDF-Next ドキュメントの基本的な構成要素を順に説明します:ドキュメントの作成、ページの追加、テキストの記述、画像の埋め込み、そして出力の生成です。

ドキュメントの作成

Document::create() 静的ファクトリは、すべての PDF の単一エントリポイントです:

php
use Yeeefang\TcpdfNext\Core\Document;

$doc = Document::create();

すべてのパラメータはオプションです。デフォルト設定では、ミリメートル単位の A4 縦向きドキュメントが生成されます。オプションの完全なリストについては設定ページを参照してください。

ページの追加

ドキュメントは空の状態から始まります。コンテンツを記述する前に、少なくとも 1 回は addPage() を呼び出してください:

php
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::A3297 × 420 mm
PageSize::A4210 × 297 mm
PageSize::A5148 × 210 mm
PageSize::Letter215.9 × 279.4 mm
PageSize::Legal215.9 × 355.6 mm

カスタムサイズは PageSize::custom(width, height) でサポートされています。

フォントの設定

TCPDF-Next は、標準的な PDF ベースフォントに加え、Unicode 対応の DejaVu Sans ファミリーを同梱しています。

php
// 組み込みベースフォント
$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 フォントを登録し、エイリアスで使用します:

php
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()

単一行のセルを出力します。ラベル、テーブルセル、短いテキストに最適です:

php
$doc->cell(
    width: 80,
    height: 10,
    text: 'Invoice #1042',
    border: true,
    align: Align::Center,
);

multiCell()

指定した幅内で自動的に折り返されるテキストを出力します。呼び出しごとにカーソルが下に移動します:

php
$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) にテキストを配置します。カーソルは移動しません

php
$doc->text(x: 105, y: 20, text: 'Centered Title', align: Align::Center);

write()

現在のカーソル位置にインラインテキストを記述します。リンクをサポートし、段落内で自然に流れます:

php
$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.');

画像

ファイルパスから

php
$doc->imageFromFile(
    path: '/images/logo.png',
    x: 15,
    y: 15,
    width: 40,
);

文字列またはリソースから

php
$binary = file_get_contents('https://example.com/photo.jpg');

$doc->image(
    data: $binary,
    x: 15,
    y: 60,
    width: 50,
    type: 'JPEG',
);

サポートされるフォーマット: PNGJPEGGIFSVGWebP

保存と出力

TCPDF-Next は、最終的な PDF を取得するためのいくつかの方法を提供しています。

ディスクに保存

php
$doc->save('/reports/invoice-1042.pdf');

ブラウザに送信

php
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 データを取得

php
$pdfBytes = $doc->getPdfData();

// PSR-7 レスポンス、キュージョブ、S3 アップロードなどで使用

OutputDestination Enum

動作
OutputDestination::Inlineインライン表示のためにブラウザに送信
OutputDestination::Downloadファイルダウンロードとしてブラウザに送信
OutputDestination::Fileファイルパスに書き出し(save() で内部使用)
OutputDestination::String生のバイナリ文字列を返却(getPdfData() で内部使用)

フルエントAPI

ほとんどのセッターは $this を返すため、チェーンによるフルエントスタイルが可能です:

php
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');

完全な例

すべてをまとめた例です:

php
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);

次のステップ

  • 設定 — デフォルト値、暗号化、アクセシビリティの微調整。
  • 設定 — Core、Pro、Artisan の連携方法の理解。
  • よくある質問 — よくある質問と回答。

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