Skip to content

텍스트 서식

텍스트 서식의 전체 범위를 보여줍니다: 폰트 패밀리, 볼드/이탤릭 스타일, 포인트 크기, RGB 색상, Alignment enum을 통한 정렬, 여러 줄 단락.

전체 예제

php
<?php

declare(strict_types=1);

require __DIR__ . '/vendor/autoload.php';

use TcpdfNext\Document;
use TcpdfNext\Enums\Alignment;

Document::create()
    ->setTitle('Text Formatting')
    ->addPage()

    // -- 폰트 패밀리 ---------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->setTextColor(33, 37, 41)
    ->cell(0, 12, 'Font Families', newLine: true)

    ->setFont('helvetica', size: 12)
    ->cell(0, 8, 'Helvetica -- clean sans-serif', newLine: true)

    ->setFont('times', size: 12)
    ->cell(0, 8, 'Times -- classic serif', newLine: true)

    ->setFont('courier', size: 12)
    ->cell(0, 8, 'Courier -- monospace', newLine: true)

    // -- 스타일 ----------------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->cell(0, 16, 'Font Styles', newLine: true)

    ->setFont('helvetica', size: 12)
    ->cell(0, 8, 'Regular text', newLine: true)

    ->setFont('helvetica', style: 'B', size: 12)
    ->cell(0, 8, 'Bold text', newLine: true)

    ->setFont('helvetica', style: 'I', size: 12)
    ->cell(0, 8, 'Italic text', newLine: true)

    ->setFont('helvetica', style: 'BI', size: 12)
    ->cell(0, 8, 'Bold + Italic text', newLine: true)

    // -- 크기 -----------------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->cell(0, 16, 'Font Sizes', newLine: true)

    ->setFont('helvetica', size: 8)
    ->cell(0, 6, '8 pt -- fine print', newLine: true)

    ->setFont('helvetica', size: 12)
    ->cell(0, 8, '12 pt -- body text', newLine: true)

    ->setFont('helvetica', size: 20)
    ->cell(0, 12, '20 pt -- subheading', newLine: true)

    ->setFontSize(28)
    ->cell(0, 16, '28 pt -- heading', newLine: true)

    // -- 색상 ----------------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->setTextColor(33, 37, 41)
    ->cell(0, 16, 'Text Colors', newLine: true)

    ->setFont('helvetica', size: 12)
    ->setTextColor(220, 53, 69)
    ->cell(0, 8, 'Red (220, 53, 69)', newLine: true)

    ->setTextColor(25, 135, 84)
    ->cell(0, 8, 'Green (25, 135, 84)', newLine: true)

    ->setTextColor(13, 110, 253)
    ->cell(0, 8, 'Blue (13, 110, 253)', newLine: true)

    ->setTextColor(33, 37, 41)

    // -- 정렬 -------------------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->cell(0, 16, 'Text Alignment', newLine: true)

    ->setFont('helvetica', size: 12)
    ->cell(0, 8, 'Left-aligned (default)', align: Alignment::Left, newLine: true)
    ->cell(0, 8, 'Center-aligned',         align: Alignment::Center, newLine: true)
    ->cell(0, 8, 'Right-aligned',          align: Alignment::Right, newLine: true)

    // -- 여러 줄 단락 --------------------------------------------
    ->setFont('helvetica', style: 'B', size: 18)
    ->cell(0, 16, 'Multi-Line Text', newLine: true)

    ->setFont('helvetica', size: 11)
    ->multiCell(
        width:  0,
        height: 7,
        text:   "TCPDF-Next wraps text automatically with multiCell().\n"
              . "Use the Alignment enum for justified, left, center, or right alignment.\n"
              . "Line spacing is controlled by the height parameter.",
        align:  Alignment::Justified,
    )

    ->save(__DIR__ . '/text-formatting.pdf');

echo 'PDF created.' . PHP_EOL;

핵심 개념

스타일 문자

문자의미
(빈값)레귤러
B볼드
I이탤릭
BI볼드 + 이탤릭

setTextColor(r, g, b)

0--255 범위의 RGB 정수를 받습니다. 텍스트 작성 메서드 전에 호출합니다:

php
->setTextColor(13, 110, 253)   // 파란색

Alignment Enum

효과
Alignment::Left왼쪽 정렬 (기본값)
Alignment::Center가운데 정렬
Alignment::Right오른쪽 정렬
Alignment::Justified양쪽 맞춤 (multiCell만 해당)

cell() vs multiCell()

  • cell() -- 단일 줄, 자동 줄바꿈 없음.
  • multiCell() -- 주어진 너비 내에서 텍스트를 자동 줄바꿈하며 양쪽 맞춤을 지원합니다.

출력

생성된 PDF는 세 가지 폰트 패밀리, 네 가지 스타일 변형, 네 가지 크기, 세 가지 색상, 세 가지 정렬 모드, 양쪽 맞춤 단락을 보여주는 한 페이지를 포함합니다.

LGPL-3.0-or-later 라이선스로 배포됩니다.