Text, fonts, and page basics
In the first tutorial you rendered a page with a single line of text. This time you take control of the page itself and of the text on it. You will pick a page size, write headings and paragraphs, and style them with the engine’s built-in fonts.
What you will build
Section titled “What you will build”You will write two small scripts. Each one produces a one-page Portable Document Format (PDF) file:
01-text-basics.phprenders an A4 portrait page with a colored heading, wrapped paragraphs, and three alignment samples.02-fonts.phprenders a sampler page that compares two built-in font families at several sizes.
Everything runs with the nextpdf/core package alone. You need no font
files, no extra extensions, and no network. If you do not have a project set
up yet, follow the first tutorial
first and come back.
Step 1: Style text on an A4 page
Section titled “Step 1: Style text on an A4 page”Every page has a size and an orientation. A4 is the common international
paper size, and Letter is its United States counterpart. Orientation is
either portrait (upright) or landscape (sideways). You pass both choices to
addPage() when you start a page.
Three methods then do the writing. setFont() picks the typeface and size
for the text that follows. cell() writes one line inside an invisible box.
multiCell() wraps longer text into as many lines as it needs. In both
methods, a width of 0 means “use everything up to the right margin”.
Two more calls handle the look. setTextColor() takes red, green, and blue
values from 0 to 255, and it applies until you change it again. The
Alignment enum, a fixed list of named choices, offers Left, Center,
Right, and Justify.
Create 01-text-basics.php in your project folder:
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use NextPDF\Contracts\Alignment;use NextPDF\Contracts\Orientation;use NextPDF\Core\Document;use NextPDF\ValueObjects\PageSize;
@mkdir(__DIR__ . '/out');
$document = Document::createStandalone();$document->setTitle('Text basics');$document->addPage(PageSize::A4(), Orientation::Portrait);
// A colored heading. The values are red, green, and blue, each 0 to 255.$document->setFont('helvetica', 'B', 20);$document->setTextColor(30, 64, 175);$document->cell(0, 14, 'Project kickoff notes', newLine: true);$document->ln(4);
// Back to black for the body text.$document->setTextColor(0, 0, 0);$document->setFont('helvetica', '', 11);$document->multiCell( 0, 7, 'Welcome to the second tutorial. This paragraph is written with ' . 'multiCell(), so the engine wraps the words onto as many lines ' . 'as the page width requires.',);$document->ln(4);
$document->multiCell( 0, 7, 'Justified text stretches the spaces so both edges line up. It is a ' . 'good fit for report bodies and other long passages.', align: Alignment::Justify,);$document->ln(6);
// One line each: left, centered, and right.$document->setFont('helvetica', 'I', 11);$document->cell(0, 8, 'Left-aligned line', newLine: true, align: Alignment::Left);$document->cell(0, 8, 'Centered line', newLine: true, align: Alignment::Center);$document->cell(0, 8, 'Right-aligned line', newLine: true, align: Alignment::Right);
$document->save(__DIR__ . '/out/text-basics.pdf');
echo "Wrote out/text-basics.pdf\n";Run it with php 01-text-basics.php. The script prints
Wrote out/text-basics.pdf, and the file appears in the new out/ folder.
What just happened
Section titled “What just happened”addPage(PageSize::A4(), Orientation::Portrait) created the page before any
text was written. Pass Orientation::Landscape instead, and the same page
turns sideways. Other named sizes, such as PageSize::A5() or
PageSize::Letter(), work the same way.
setFont('helvetica', 'B', 20) selected bold Helvetica at 20 points. A
point (pt) is the traditional print unit; 72 points equal one inch. The
heading is blue because setTextColor(30, 64, 175) was active when the
cell() call wrote it. The next setTextColor(0, 0, 0) switched back to
black for everything after it.
The two multiCell() calls wrapped their paragraphs automatically. The
justified one stretches word spacing so both edges align, except on the
final line of the paragraph. Between blocks, ln() moved the write position
down to add breathing room. Finally, save() built the file and wrote it
into out/.
For every parameter these methods accept, see Compose text with fonts and alignment.
Step 2: Choose among the built-in fonts
Section titled “Step 2: Choose among the built-in fonts”The engine ships three text families built in: Helvetica, Times, and Courier. Helvetica is a sans-serif face, meaning its letters have no small end-strokes (serifs). Times is a serif face that reads well in long, print-like passages. Courier is fixed width, so it suits code listings and receipts. Because these families are built in, your scripts need no font files at all.
The style argument of setFont() combines single-letter flags: 'B' for
bold, 'I' for italic, and 'U' for underline. An empty string means
regular, and flags combine, so 'BI' gives bold italic.
The built-in families cover Latin-based text. For other writing systems or your own brand typeface, you register a font file, as the Embed and subset fonts recipe (subsetting keeps only the characters you actually used, so files stay small) shows. The font support matrix lists every font format the engine accepts.
Create 02-fonts.php next to the first script:
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use NextPDF\Core\Document;
@mkdir(__DIR__ . '/out');
$document = Document::createStandalone();$document->setTitle('Font sampler');$document->addPage();
$document->setFont('helvetica', 'B', 20);$document->cell(0, 14, 'Built-in font sampler', newLine: true);$document->ln(4);
// Helvetica: a sans-serif face, a solid default for labels and headings.$document->setFont('helvetica', 'B', 14);$document->cell(0, 10, 'Helvetica', newLine: true);foreach ([10, 12, 16] as $size) { $document->setFont('helvetica', '', (float) $size); $document->cell(0, $size * 0.8, "Helvetica sample at {$size}pt.", newLine: true);}$document->setFont('helvetica', 'BI', 12);$document->cell(0, 9, 'Helvetica bold italic for emphasis.', newLine: true);$document->ln(6);
// Times: a serif face that suits long, print-like passages.$document->setFont('times', 'B', 14);$document->cell(0, 10, 'Times', newLine: true);foreach ([10, 12, 16] as $size) { $document->setFont('times', '', (float) $size); $document->cell(0, $size * 0.8, "Times sample at {$size}pt.", newLine: true);}$document->setFont('times', 'I', 12);$document->cell(0, 9, 'Times italic for quotations.', newLine: true);
$document->save(__DIR__ . '/out/font-sampler.pdf');
echo "Wrote out/font-sampler.pdf\n";Run it with php 02-fonts.php and open out/font-sampler.pdf. You see the
same sample sentence change character between the two families and grow
through three sizes.
What just happened
Section titled “What just happened”addPage() with no arguments used the default page setup in portrait
orientation, so not every script has to spell the size out. Each
setFont() call switched the active family, style, or size mid-page, and a
font stays active until the next setFont() call.
The loops cast the size with (float) because setFont() expects a decimal
number. The line heights scale with the font size ($size * 0.8), so larger
text gets taller lines and nothing overlaps. Try replacing 'times' with
'courier' and run the script again to see the third family.
If something went wrong
Section titled “If something went wrong”- A “class not found” or autoload error usually means the script did not
find
vendor/autoload.php. Run it inside the project folder that containsvendor/. - A mistyped family name throws an exception that names the font it could
not find. Check the spelling:
helvetica,times,courier. - The troubleshooting hub collects common fixes, and Fonts and tagging covers font problems in depth.
- Every engine exception is documented in the error reference, with the context it carries and the recovery action.
You can now shape pages, style text, and pick fonts with intent. In the next tutorial you place images on the page and organize content with tables and lists.