Hello World
Contoh TCPDF-Next paling sederhana: buat dokumen, tambahkan halaman, tulis teks, dan simpan -- semua dalam satu rantai fluent.
Contoh Lengkap
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 secara default
->setFont('helvetica', size: 16)
->cell(
width: 0, // 0 = lebar cetak penuh
height: 10,
text: 'Hello World!',
align: Alignment::Center,
)
->save(__DIR__ . '/hello-world.pdf');
echo 'PDF created.' . PHP_EOL;Fungsi Setiap Method
| Method | Tujuan |
|---|---|
Document::create() | Factory statis -- mengembalikan Document baru dengan default A4 / portrait / mm |
setAuthor(), setTitle(), setSubject() | Embed metadata yang terlihat di panel properti reader |
addPage() | Sisipkan halaman (diperlukan sebelum konten apa pun) |
setFont(family, size) | Aktifkan family font dan ukuran poin |
cell(width, height, text, align) | Tulis sel teks satu baris |
save(path) | Serialisasi PDF dan tulis ke disk |
Mode Output Alternatif
php
use TcpdfNext\Enums\OutputDestination;
// Kembalikan byte PDF mentah sebagai string
$bytes = $pdf->output(OutputDestination::String);
// Kirim inline ke browser
$pdf->output(OutputDestination::Inline, 'hello.pdf');Output
Menjalankan script menghasilkan PDF A4 satu halaman dengan "Hello World!" di tengah dekat bagian atas halaman.
TIP
Fluent API berarti setiap setter mengembalikan static -- tidak perlu variabel perantara.