텍스트 출력 (HasTextOutput)
HasTextOutput 트레이트는 페이지에 텍스트를 배치하기 위한 주요 메서드를 제공합니다. 각 메서드는 서로 다른 레이아웃 모델을 지원합니다: 고정 셀, 흐르는 텍스트, 절대 위치 지정, HTML 렌더링.
빠른 참조
| 메서드 | 레이아웃 모델 | 줄 바꿈 |
|---|---|---|
cell() | 단일 행 박스 | 아니오 — 텍스트가 잘림 |
multiCell() | 다중 행 블록 | 예 — 셀 너비에서 자동 줄 바꿈 |
text() | 절대 위치 | 아니오 |
write() | 인라인 흐름 | 예 — 워드 프로세서처럼 흐름 |
writeHtml() | HTML 블록 | 예 — 완전한 HTML 레이아웃 |
writeHtmlCell() | 위치 지정된 셀 내 HTML | 예 |
ln() | 줄 바꿈 | 해당 없음 |
기본 예제
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Single line cell', newLine: true)
->multiCell(0, 10, 'This is a longer text that will automatically wrap to multiple lines when it reaches the edge of the printable area.')
->ln(5)
->text(50.0, 100.0, 'Absolute positioned text')
->write(10, 'Inline text that ')
->write(10, 'continues flowing.');모든 메서드는 static을 반환하므로 모든 호출을 체이닝할 수 있습니다.
cell()
선택적 테두리, 배경 채우기, 정렬, 링크를 포함한 단일 행 사각 셀을 그립니다.
$pdf->cell(
float $w, // 너비 (0 = 오른쪽 여백까지 확장)
float $h, // 높이
string $txt, // 텍스트 내용
mixed $border = 0, // 0, 1, 또는 'LTRB' 조합
bool $newLine = false,
string $align = '', // L, C, R, J
bool $fill = false,
mixed $link = '',
);커서는 셀의 오른쪽으로 이동합니다($newLine이 true이면 다음 줄로 이동합니다).
정렬
use Yeeefang\TcpdfNext\Contracts\Enums\Alignment;
$pdf->cell(0, 10, 'Left aligned')
->ln()
->cell(0, 10, 'Centered', align: 'C')
->ln()
->cell(0, 10, 'Right aligned', align: 'R')
->ln()
->cell(0, 10, 'Justified text in a cell', align: 'J');테두리
// 테두리 없음
$pdf->cell(60, 10, 'No border', border: 0);
// 전체 프레임
$pdf->cell(60, 10, 'Full frame', border: 1);
// 개별 면 — Left, Top, Right, Bottom
$pdf->cell(60, 10, 'Top and bottom', border: 'TB');
$pdf->cell(60, 10, 'Left only', border: 'L');채우기
$pdf->setFillColor(230, 230, 250)
->cell(0, 10, 'Lavender background', fill: true, newLine: true);multiCell()
지정된 너비에서 자동으로 줄 바꿈되는 다중 행 텍스트 블록을 렌더링합니다. 출력 후 커서는 셀 아래로 이동합니다.
$pdf->multiCell(
float $w, // 너비 (0 = 오른쪽 여백까지 확장)
float $h, // 최소 줄 높이
string $txt, // 텍스트 내용
mixed $border = 0,
string $align = 'J',
bool $fill = false,
);$pdf->setFont('Helvetica', '', 11)
->multiCell(80, 6, 'This paragraph will wrap at 80mm width. The text flows naturally, respecting word boundaries and hyphenation rules.', border: 1, align: 'J');text()
절대 (x, y) 위치에 단일 문자열을 배치합니다. 이후 커서는 이동하지 않습니다 — 이것은 "실행 후 잊는" 메서드입니다.
$pdf->text(20.0, 50.0, 'Positioned at x=20, y=50');정확한 위치를 직접 제어하는 워터마크, 라벨 또는 오버레이 콘텐츠에 text()를 사용하십시오.
write()
현재 커서 위치에서 인라인으로 텍스트를 작성합니다. 텍스트가 오른쪽 여백에 도달하면 워드 프로세서에서 타이핑하는 것처럼 자동으로 줄 바꿈됩니다.
$pdf->setFont('Helvetica', '', 12)
->write(6, 'This sentence starts here and ')
->setFont('Helvetica', 'B', 12)
->write(6, 'this part is bold')
->setFont('Helvetica', '', 12)
->write(6, ' then back to normal.');첫 번째 매개변수($h)는 줄 높이입니다. 문장 중간에서 폰트나 스타일을 변경해야 할 때 write()를 사용하십시오.
writeHtml()
내장 HTML 파서를 사용하여 HTML 문자열을 렌더링합니다. 제목, 단락, 테이블, 리스트, 인라인 스타일을 포함한 일반적인 태그를 지원합니다.
$pdf->writeHtml('<h2>Section Title</h2><p>Paragraph with <b>bold</b> and <i>italic</i> text.</p>');테이블
$html = '
<table border="1" cellpadding="4">
<thead>
<tr>
<th>Product</th>
<th>Qty</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr>
<td>Widget A</td>
<td align="center">10</td>
<td align="right">$25.00</td>
</tr>
<tr>
<td>Widget B</td>
<td align="center">5</td>
<td align="right">$42.50</td>
</tr>
</tbody>
</table>';
$pdf->writeHtml($html);writeHtmlCell()
HTML 렌더링과 셀 위치 지정을 결합합니다. HTML 콘텐츠가 지정된 좌표의 셀 안에 배치됩니다.
$pdf->writeHtmlCell(
float $w, // 너비
float $h, // 최소 높이
float $x, // X 위치
float $y, // Y 위치
string $html,
mixed $border = 0,
bool $fill = false,
);$pdf->writeHtmlCell(90, 0, 10, 50, '<p style="color:#336699;">Positioned HTML content with <b>formatting</b>.</p>', border: 1);ln()
줄 바꿈을 삽입합니다. 커서가 왼쪽 여백으로 이동하고 지정된 높이만큼 아래로 내려갑니다.
$pdf->ln(); // 마지막 셀 높이를 사용한 줄 바꿈
$pdf->ln(10); // 10mm 수직 간격의 줄 바꿈
$pdf->ln(0); // 수직 이동 없이 왼쪽 여백으로 이동적절한 메서드 선택
| 시나리오 | 메서드 |
|---|---|
| 테이블 셀, 라벨, 단일 행 데이터 | cell() |
| 단락, 설명, 긴 텍스트 | multiCell() |
| 워터마크, 스탬프, 절대 위치 라벨 | text() |
| 혼합 서식의 인라인 텍스트 | write() |
| HTML 마크업이 있는 리치 콘텐츠 | writeHtml() |
| 특정 위치의 HTML 콘텐츠 | writeHtmlCell() |