Skip to content

Grundlegende Verwendung

Diese Seite führt durch die fundamentalen Bausteine jedes TCPDF-Next-Dokuments: Erstellen eines Dokuments, Hinzufügen von Seiten, Schreiben von Text, Einbetten von Bildern und Erzeugen der Ausgabe.

Erstellen eines Dokuments

The Document::create() static factory is the single entry point for every PDF:

php
use Yeeefang\TcpdfNext\Core\Document;

$doc = Document::create();

All parameters are optional — the defaults give you an A4 portrait document measured in millimetres. See the Konfiguration page for the full list of options.

Hinzufügen von Seiten

A document starts empty. Call addPage() at least once before writing any content:

php
use Yeeefang\TcpdfNext\Core\Enums\PageSize;
use Yeeefang\TcpdfNext\Core\Enums\Orientation;
use Yeeefang\TcpdfNext\Core\ValueObjects\Margin;

// Add a page with the document defaults
$doc->addPage();

// Add a landscape Letter page with custom margins
$doc->addPage(
    pageSize: PageSize::Letter,
    orientation: Orientation::Landscape,
    margin: new Margin(left: 10, top: 10, right: 10, bottom: 10),
);

Verfügbare Seitengrößen

PageSize is a backed enum that includes all standard ISO and North American sizes:

Enum ValueDimensions
PageSize::A3297 × 420 mm
PageSize::A4210 × 297 mm
PageSize::A5148 × 210 mm
PageSize::Letter215.9 × 279.4 mm
PageSize::Legal215.9 × 355.6 mm

Custom sizes are supported via PageSize::custom(width, height).

Schriftarten setzen

TCPDF-Next bundles the standard PDF base fonts plus the Unicode-capable DejaVu Sans family.

php
// Built-in base font
$doc->setFont('Helvetica', size: 12);

// Built-in Unicode font
$doc->setFont('DejaVuSans', size: 10);

// Bold / Italic variants
$doc->setFont('Helvetica', style: FontStyle::Bold, size: 14);
$doc->setFont('Helvetica', style: FontStyle::BoldItalic, size: 14);

Benutzerdefinierte Schriftarten

Register a TrueType or OpenType font, then use it by its alias:

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

Textausgabe

TCPDF-Next provides four methods for placing text on a page. Each serves a different layout need.

cell()

Prints a single-line cell. Ideal for labels, table cells, and short text:

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

multiCell()

Prints text that wraps automatically within a given width. The cursor moves down after each call:

php
$doc->multiCell(
    width: 0,        // 0 = full available width
    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()

Places text at an absolute (x, y) position. Does not move the cursor:

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

write()

Writes inline text at the current cursor position. Supports links and flows naturally within a paragraph:

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

Bilder

Von einem Dateipfad

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

Von einem String oder einer Ressource

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

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

Supported formats: PNG, JPEG, GIF, SVG, WebP.

Speichern und Ausgabe

TCPDF-Next offers several ways to retrieve the final PDF.

Auf Disk speichern

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

An Browser senden

php
use Yeeefang\TcpdfNext\Core\Enums\OutputDestination;

// Inline display (Content-Disposition: inline)
$doc->output('invoice.pdf', OutputDestination::Inline);

// Force download (Content-Disposition: attachment)
$doc->output('invoice.pdf', OutputDestination::Download);

Rohe PDF-Daten abrufen

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

// Use with a PSR-7 response, a queue job, S3 upload, etc.

AusgabeDestination Enum

ValueBehaviour
AusgabeDestination::InlineSends to browser for inline viewing
AusgabeDestination::DownloadSends to browser as a file download
AusgabeDestination::FileWrites to a file path (used internally by save())
AusgabeDestination::StringReturns raw binary string (used internally by getPdfData())

Fluent API

Most setters return $this, enabling a chained, fluent style:

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

Vollständiges Beispiel

Putting it all together:

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

// Header
$doc->setFont('Helvetica', style: FontStyle::Bold, size: 20);
$doc->cell(width: 0, height: 15, text: 'Hello, TCPDF-Next!', align: Align::Center);
$doc->ln(20);

// Body
$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.',
);

// Logo
$doc->imageFromFile(path: __DIR__ . '/logo.png', x: 15, y: 80, width: 30);

// Output
$doc->output('hello.pdf', OutputDestination::Download);

Nächste Schritte

Veröffentlicht unter der LGPL-3.0-or-later Lizenz.