Typography (HasTypography)
The HasTypography trait controls the visual appearance of text: font family, style, size, spacing, stretching, color, shadow, and rendering mode. All methods return static for fluent chaining.
Setting Fonts
setFont()
Set the current font family, style, and size in a single call.
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Normal text', newLine: true)
->setFont('Helvetica', 'B', 14)
->cell(0, 10, 'Bold text', newLine: true)
->setFont('Helvetica', 'BI', 16)
->cell(0, 10, 'Bold Italic text', newLine: true);Font Styles
Combine style characters to apply multiple effects:
| Code | Style |
|---|---|
'' | Regular |
'B' | Bold |
'I' | Italic |
'BI' | Bold Italic |
'U' | Underline |
'D' | Strikethrough |
'O' | Overline |
Styles can be combined freely: 'BIU' produces bold, italic, underlined text.
$pdf->setFont('Helvetica', 'BU', 12)
->cell(0, 10, 'Bold + Underline', newLine: true)
->setFont('Helvetica', 'ID', 12)
->cell(0, 10, 'Italic + Strikethrough', newLine: true)
->setFont('Helvetica', 'BO', 12)
->cell(0, 10, 'Bold + Overline', newLine: true);setFontSize()
Change the font size without switching family or style.
$pdf->setFont('Helvetica', 'B', 12)
->cell(0, 10, '12pt heading', newLine: true)
->setFontSize(10)
->cell(0, 10, '10pt body text', newLine: true)
->setFontSize(8)
->cell(0, 10, '8pt footnote', newLine: true);Spacing and Stretching
setFontSpacing()
Adjusts the extra space (in points) inserted between every pair of characters.
$pdf->setFont('Helvetica', '', 12)
->setFontSpacing(0)
->cell(0, 10, 'Normal spacing', newLine: true)
->setFontSpacing(1.5)
->cell(0, 10, 'Expanded spacing', newLine: true)
->setFontSpacing(-0.5)
->cell(0, 10, 'Tight spacing', newLine: true)
->setFontSpacing(0);setFontStretching()
Applies horizontal scaling to glyphs. A value of 100 is normal width.
$pdf->setFont('Helvetica', '', 14)
->setFontStretching(100)
->cell(0, 10, 'Normal width (100%)', newLine: true)
->setFontStretching(130)
->cell(0, 10, 'Stretched (130%)', newLine: true)
->setFontStretching(75)
->cell(0, 10, 'Condensed (75%)', newLine: true)
->setFontStretching(100);Measuring Text
getStringWidth()
Returns the width (in user units) of a string rendered in the current font and size. Useful for calculating layout before drawing.
$pdf->setFont('Helvetica', '', 12);
$width = $pdf->getStringWidth('Invoice Total: $1,250.00');
// Use the measured width to right-align a cell
$pageWidth = $pdf->getPageWidth();
$rightMargin = $pdf->getRightMargin();
$pdf->setX($pageWidth - $rightMargin - $width)
->cell($width, 10, 'Invoice Total: $1,250.00');Text Rendering Modes
PDF defines 8 text rendering modes via the TextRenderer enum. These control whether text is filled, stroked, used as a clipping path, or hidden.
| Mode | Value | Effect |
|---|---|---|
| Fill | 0 | Normal filled text (default) |
| Stroke | 1 | Outline only |
| FillStroke | 2 | Filled with outline |
| Invisible | 3 | Hidden but selectable/searchable |
| FillClip | 4 | Fill, then add to clipping path |
| StrokeClip | 5 | Stroke, then add to clipping path |
| FillStrokeClip | 6 | Fill + stroke, then clip |
| Clip | 7 | Add to clipping path only |
$pdf->setFont('Helvetica', 'B', 36)
->setTextRenderingMode(0)
->cell(0, 15, 'Filled text', newLine: true)
->setTextRenderingMode(1)
->cell(0, 15, 'Outlined text', newLine: true)
->setTextRenderingMode(2)
->cell(0, 15, 'Fill + Stroke', newLine: true)
->setTextRenderingMode(3)
->cell(0, 15, 'Invisible (but searchable)', newLine: true)
->setTextRenderingMode(0);The Invisible mode (3) is especially useful for OCR overlays — you place recognized text on top of a scanned image so the PDF is searchable while the image remains visible.
Text Color
setTextColor()
Set the text color using RGB values.
$pdf->setTextColor(0, 0, 0) // Black
->cell(0, 10, 'Black text', newLine: true)
->setTextColor(220, 50, 50) // Red
->cell(0, 10, 'Red text', newLine: true)
->setTextColor(0, 102, 204) // Blue
->cell(0, 10, 'Blue text', newLine: true)
->setTextColor(0, 0, 0); // Reset to blackWhen called with a single argument, it sets a grayscale value (0 = black, 255 = white).
$pdf->setTextColor(128)
->cell(0, 10, 'Gray text', newLine: true);Text Shadow
setTextShadow()
Apply a shadow effect beneath text. The shadow is defined as an associative array.
$pdf->setFont('Helvetica', 'B', 28)
->setTextShadow([
'enabled' => true,
'depth_w' => 0.4, // Horizontal offset (mm)
'depth_h' => 0.4, // Vertical offset (mm)
'color' => [180, 180, 180],
'opacity' => 0.5,
'blend_mode' => 'Normal',
])
->cell(0, 15, 'Heading with Shadow', newLine: true)
->setTextShadow(['enabled' => false]);Complete Example
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
// Title — large, bold, blue
->setFont('Helvetica', 'B', 24)
->setTextColor(0, 51, 102)
->cell(0, 14, 'Monthly Report', newLine: true)
// Subtitle — regular, gray, with extra spacing
->setFont('Helvetica', '', 12)
->setTextColor(100, 100, 100)
->setFontSpacing(1.0)
->cell(0, 10, 'February 2026', newLine: true)
->setFontSpacing(0)
->ln(5)
// Body text — black, normal
->setFont('Times', '', 11)
->setTextColor(0, 0, 0)
->multiCell(0, 6, 'Revenue increased 12% compared to the previous quarter. Operating costs remained stable, and net margin improved by 3 percentage points.')
->ln(3)
// Footnote — small, italic, gray
->setFont('Times', 'I', 8)
->setTextColor(150, 150, 150)
->cell(0, 5, '* All figures are unaudited.')
->save('report.pdf');