Skip to content

Trường Form

Module Form (FormFieldManager, FormField) tạo trường AcroForm tương tác trong PDF. Loại trường được định nghĩa bởi enum FormFieldType: TEXT, PASSWORD, TEXTAREA, CHECKBOX, RADIO, LISTBOX, COMBOBOX, BUTTON. Mọi method trả về static, nên mỗi lệnh gọi có thể chain.

Tham chiếu nhanh

MethodLoại trường
textField()Input text một dòng
checkboxField()Checkbox bật/tắt
radioField()Radio button (nhóm)
listboxField()Danh sách cuộn
comboboxField()Dropdown selector
buttonField()Nút bấm với tùy chọn JavaScript action
flattenFields()Chuyển mọi trường tương tác thành nội dung tĩnh

Ví dụ cơ bản

php
use Yeeefang\TcpdfNext\Core\Document;

$pdf = Document::create()
    ->addPage()
    ->setFont('Helvetica', '', 12)
    ->cell(0, 10, 'Registration Form', newLine: true)
    ->ln(5)

    // Text field
    ->cell(30, 8, 'Name:')
    ->textField('name', 45, null, 80, 8, [
        'border'    => ['width' => 1],
        'maxLength' => 100,
    ])
    ->ln(12)

    // Email field
    ->cell(30, 8, 'Email:')
    ->textField('email', 45, null, 80, 8)
    ->ln(12)

    // Checkbox
    ->checkboxField('agree', 15, null, 5, 5)
    ->cell(0, 5, '  I agree to the terms', newLine: true)
    ->ln(10)

    // Dropdown
    ->cell(30, 8, 'Country:')
    ->comboboxField('country', 45, null, 60, 8, ['US', 'UK', 'TW', 'JP', 'DE'])
    ->ln(12)

    // Nút submit
    ->buttonField('submit', 45, null, 40, 10, 'Submit', 'submitForm("https://example.com/submit")');

Trường Text và Checkbox

php
$pdf->textField(string $name, float $x, float $y, float $w, float $h, array $prop = []);
$pdf->checkboxField(string $name, float $x, float $y, float $w, float $h, bool $checked = false);

Truyền null cho $y để dùng vị trí con trỏ dọc hiện tại.

Trường Radio Button

php
$pdf->radioField(string $name, float $x, float $y, float $w, float $h, array $prop = []);

Radio button cùng $name tạo thành nhóm loại trừ lẫn nhau:

php
$pdf->cell(30, 8, 'Gender:')
    ->radioField('gender', 45, null, 5, 5, ['value' => 'male'])
    ->cell(10, 5, ' M')
    ->radioField('gender', 65, null, 5, 5, ['value' => 'female'])
    ->cell(10, 5, ' F');

Trường List, Combo Box và Button

php
$pdf->listboxField(string $name, float $x, float $y, float $w, float $h, array $values, array $prop = []);
$pdf->comboboxField(string $name, float $x, float $y, float $w, float $h, array $values, array $prop = []);
$pdf->buttonField(string $name, float $x, float $y, float $w, float $h, string $caption, string $action = '');

listboxField() render danh sách cuộn nhiều hàng. comboboxField() render dropdown một hàng. buttonField() tạo nút bấm với chuỗi JavaScript action tùy chọn.

Thuộc tính trường

Mảng $prop kiểm soát giao diện và hành vi trường:

KeyKiểuMô tả
borderarrayStyle border với key width, color, style
bgcolorarrayMàu nền dạng [r, g, b]
fontstringTên họ font
fontSizefloatKích thước font tính bằng point
alignmentstringCăn chỉnh text: left, center, right
maxLengthintSố ký tự tối đa (text field)
readonlyboolNgăn người dùng chỉnh sửa
requiredboolĐánh dấu bắt buộc cho form validation
valuestringGiá trị mặc định / ban đầu

Form Flattening

Flattening chuyển mọi trường tương tác thành nội dung tĩnh, không chỉnh sửa được. Hữu ích cho lưu trữ form đã hoàn thành hoặc tạo PDF chỉ đọc cuối cùng.

php
$pdf->flattenFields();  // Chuyển mọi trường form thành nội dung tĩnh

Sau khi flatten, giá trị trường trở thành văn bản cố định. Trường không thể chỉnh sửa trong trình xem PDF nữa.

Phân phối theo giấy phép LGPL-3.0-or-later.