타이포그래피 (HasTypography)
HasTypography 트레이트는 텍스트의 시각적 외관을 제어합니다: 폰트 패밀리, 스타일, 크기, 자간, 장평, 색상, 그림자, 렌더링 모드. 모든 메서드는 플루언트 체이닝을 위해 static을 반환합니다.
폰트 설정
setFont()
단일 호출로 현재 폰트 패밀리, 스타일, 크기를 설정합니다.
php
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);폰트 스타일
스타일 문자를 조합하여 여러 효과를 적용합니다:
| 코드 | 스타일 |
|---|---|
'' | 레귤러 |
'B' | 볼드 |
'I' | 이탤릭 |
'BI' | 볼드 이탤릭 |
'U' | 밑줄 |
'D' | 취소선 |
'O' | 윗줄 |
스타일은 자유롭게 조합할 수 있습니다: 'BIU'는 볼드, 이탤릭, 밑줄이 적용된 텍스트를 생성합니다.
php
$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()
패밀리나 스타일을 변경하지 않고 폰트 크기만 변경합니다.
php
$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);자간 및 장평
setFontSpacing()
모든 문자 쌍 사이에 삽입되는 추가 간격(포인트 단위)을 조정합니다.
php
$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()
글리프에 수평 스케일링을 적용합니다. 값 100이 정상 너비입니다.
php
$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);텍스트 측정
getStringWidth()
현재 폰트와 크기로 렌더링된 문자열의 너비(사용자 단위)를 반환합니다. 그리기 전에 레이아웃을 계산하는 데 유용합니다.
php
$pdf->setFont('Helvetica', '', 12);
$width = $pdf->getStringWidth('Invoice Total: $1,250.00');
// 측정된 너비를 사용하여 셀을 오른쪽 정렬
$pageWidth = $pdf->getPageWidth();
$rightMargin = $pdf->getRightMargin();
$pdf->setX($pageWidth - $rightMargin - $width)
->cell($width, 10, 'Invoice Total: $1,250.00');텍스트 렌더링 모드
PDF는 TextRenderer 열거형을 통해 8가지 텍스트 렌더링 모드를 정의합니다. 이는 텍스트가 채워지는지, 선으로 그려지는지, 클리핑 경로로 사용되는지, 숨겨지는지를 제어합니다.
| 모드 | 값 | 효과 |
|---|---|---|
| Fill | 0 | 일반 채워진 텍스트 (기본값) |
| Stroke | 1 | 윤곽선만 |
| FillStroke | 2 | 윤곽선이 있는 채워진 텍스트 |
| Invisible | 3 | 숨겨지지만 선택/검색 가능 |
| FillClip | 4 | 채우기 후 클리핑 경로에 추가 |
| StrokeClip | 5 | 선 후 클리핑 경로에 추가 |
| FillStrokeClip | 6 | 채우기 + 선 후 클리핑 |
| Clip | 7 | 클리핑 경로에만 추가 |
php
$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);Invisible 모드(3)는 OCR 오버레이에 특히 유용합니다 — 스캔된 이미지 위에 인식된 텍스트를 배치하면 이미지는 그대로 보이면서 PDF를 검색할 수 있게 됩니다.
텍스트 색상
setTextColor()
RGB 값을 사용하여 텍스트 색상을 설정합니다.
php
$pdf->setTextColor(0, 0, 0) // 검정
->cell(0, 10, 'Black text', newLine: true)
->setTextColor(220, 50, 50) // 빨강
->cell(0, 10, 'Red text', newLine: true)
->setTextColor(0, 102, 204) // 파랑
->cell(0, 10, 'Blue text', newLine: true)
->setTextColor(0, 0, 0); // 검정으로 초기화인수 하나만 제공하면 그레이스케일 값으로 설정됩니다(0 = 검정, 255 = 흰색).
php
$pdf->setTextColor(128)
->cell(0, 10, 'Gray text', newLine: true);텍스트 그림자
setTextShadow()
텍스트 아래에 그림자 효과를 적용합니다. 그림자는 연관 배열로 정의됩니다.
php
$pdf->setFont('Helvetica', 'B', 28)
->setTextShadow([
'enabled' => true,
'depth_w' => 0.4, // 수평 오프셋 (mm)
'depth_h' => 0.4, // 수직 오프셋 (mm)
'color' => [180, 180, 180],
'opacity' => 0.5,
'blend_mode' => 'Normal',
])
->cell(0, 15, 'Heading with Shadow', newLine: true)
->setTextShadow(['enabled' => false]);완전한 예제
php
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
// 제목 — 크고, 볼드, 파란색
->setFont('Helvetica', 'B', 24)
->setTextColor(0, 51, 102)
->cell(0, 14, 'Monthly Report', newLine: true)
// 부제목 — 레귤러, 회색, 추가 자간
->setFont('Helvetica', '', 12)
->setTextColor(100, 100, 100)
->setFontSpacing(1.0)
->cell(0, 10, 'February 2026', newLine: true)
->setFontSpacing(0)
->ln(5)
// 본문 텍스트 — 검정, 일반
->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)
// 각주 — 작고, 이탤릭, 회색
->setFont('Times', 'I', 8)
->setTextColor(150, 150, 150)
->cell(0, 5, '* All figures are unaudited.')
->save('report.pdf');