Basic Usage
This page walks through the fundamental building blocks of every TCPDF-Next document: creating a document, adding pages, writing text, embedding images, and producing output.
Creating a Document
The Document::create() static factory is the single entry point for every PDF:
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 Configuration page for the full list of options.
Adding Pages
A document starts empty. Call addPage() at least once before writing any content:
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),
);Available Page Sizes
PageSize is a backed enum that includes all standard ISO and North American sizes:
| Enum Value | Dimensions |
|---|---|
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 |
Custom sizes are supported via PageSize::custom(width, height).
Setting Fonts
TCPDF-Next bundles the standard PDF base fonts plus the Unicode-capable DejaVu Sans family.
// 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);Custom Fonts
Register a TrueType or OpenType font, then use it by its alias:
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);Text Output
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:
$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:
$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:
$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:
$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.');Images
From a File Path
$doc->imageFromFile(
path: '/images/logo.png',
x: 15,
y: 15,
width: 40,
);From a String or Resource
$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.
Saving and Output
TCPDF-Next offers several ways to retrieve the final PDF.
Save to Disk
$doc->save('/reports/invoice-1042.pdf');Send to Browser
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);Get Raw PDF Data
$pdfBytes = $doc->getPdfData();
// Use with a PSR-7 response, a queue job, S3 upload, etc.OutputDestination Enum
| Value | Behaviour |
|---|---|
OutputDestination::Inline | Sends to browser for inline viewing |
OutputDestination::Download | Sends to browser as a file download |
OutputDestination::File | Writes to a file path (used internally by save()) |
OutputDestination::String | Returns raw binary string (used internally by getPdfData()) |
Fluent API
Most setters return $this, enabling a chained, fluent style:
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');Complete Example
Putting it all together:
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);Next Steps
- Configuration — fine-tune defaults, encryption, and accessibility.
- Configuration — understand how Core, Pro, and Artisan fit together.
- FAQ — common questions answered.