Hello World
The simplest TCPDF-Next example: create a document, add a page, write text, and save -- all in one fluent chain.
Full Example
php
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use TcpdfNext\Document;
use TcpdfNext\Enums\Alignment;
Document::create()
->setAuthor('TCPDF-Next')
->setTitle('Hello World Example')
->setSubject('Simplest possible PDF')
->addPage() // A4 portrait by default
->setFont('helvetica', size: 16)
->cell(
width: 0, // 0 = full printable width
height: 10,
text: 'Hello World!',
align: Alignment::Center,
)
->save(__DIR__ . '/hello-world.pdf');
echo 'PDF created.' . PHP_EOL;What Each Method Does
| Method | Purpose |
|---|---|
Document::create() | Static factory -- returns a new Document with A4 / portrait / mm defaults |
setAuthor(), setTitle(), setSubject() | Embed metadata visible in the reader's properties panel |
addPage() | Insert a page (required before any content) |
setFont(family, size) | Activate a font family and point size |
cell(width, height, text, align) | Write a single-line text cell |
save(path) | Serialize the PDF and write it to disk |
Alternative Output Modes
php
use TcpdfNext\Enums\OutputDestination;
// Return raw PDF bytes as a string
$bytes = $pdf->output(OutputDestination::String);
// Send inline to the browser
$pdf->output(OutputDestination::Inline, 'hello.pdf');Output
Running the script produces a single-page A4 PDF with "Hello World!" centered near the top of the page.
TIP
The fluent API means every setter returns static -- no intermediate variables needed.