Định dạng văn bản
Minh họa toàn bộ phạm vi định dạng văn bản: font family, kiểu bold/italic, point size, màu RGB, căn lề qua enum Alignment, và đoạn văn nhiều dòng.
Ví dụ đầy đủ
php
<?php
declare(strict_types=1);
require __DIR__ . '/vendor/autoload.php';
use TcpdfNext\Document;
use TcpdfNext\Enums\Alignment;
Document::create()
->setTitle('Text Formatting')
->addPage()
// -- Font Family ---------------------------------------------------
->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)
// -- Kiểu ----------------------------------------------------------
->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)
// -- Kích thước -----------------------------------------------------------
->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)
// -- Màu ----------------------------------------------------------
->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)
// -- Căn lề -------------------------------------------------------
->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)
// -- Đoạn văn nhiều dòng --------------------------------------------
->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;Khái niệm chính
Ký tự kiểu
| Ký tự | Ý nghĩa |
|---|---|
| (trống) | Regular |
B | Bold |
I | Italic |
BI | Bold + Italic |
setTextColor(r, g, b)
Nhận integer RGB 0--255. Gọi trước bất kỳ method viết văn bản nào:
php
->setTextColor(13, 110, 253) // blueAlignment Enum
| Giá trị | Hiệu ứng |
|---|---|
Alignment::Left | Căn trái (mặc định) |
Alignment::Center | Căn giữa |
Alignment::Right | Căn phải |
Alignment::Justified | Căn đều (chỉ multiCell) |
cell() vs multiCell()
cell()-- một dòng, không xuống dòng.multiCell()-- xuống dòng tự động trong chiều rộng cho trước và hỗ trợ căn đều.
Kết quả
PDF được tạo chứa một trang trình bày ba font family, bốn biến thể kiểu, bốn kích thước, ba màu, ba chế độ căn lề, và một đoạn văn căn đều.