폼 필드
폼 모듈(FormFieldManager, FormField)은 PDF 내부에 인터랙티브 AcroForm 필드를 생성합니다. 필드 유형은 FormFieldType 열거형으로 정의됩니다: TEXT, PASSWORD, TEXTAREA, CHECKBOX, RADIO, LISTBOX, COMBOBOX, BUTTON. 모든 메서드는 static을 반환하므로 모든 호출을 체이닝할 수 있습니다.
빠른 참조
| 메서드 | 필드 유형 |
|---|---|
textField() | 한 줄 텍스트 입력 |
checkboxField() | 체크박스 토글 |
radioField() | 라디오 버튼 (그룹화) |
listboxField() | 스크롤 가능한 목록 |
comboboxField() | 드롭다운 선택기 |
buttonField() | 선택적 JavaScript 액션이 있는 푸시 버튼 |
flattenFields() | 모든 인터랙티브 필드를 정적 콘텐츠로 변환 |
기본 예제
php
use Yeeefang\TcpdfNext\Core\Document;
$pdf = Document::create()
->addPage()
->setFont('Helvetica', '', 12)
->cell(0, 10, 'Registration Form', newLine: true)
->ln(5)
// 텍스트 필드
->cell(30, 8, 'Name:')
->textField('name', 45, null, 80, 8, [
'border' => ['width' => 1],
'maxLength' => 100,
])
->ln(12)
// 이메일 필드
->cell(30, 8, 'Email:')
->textField('email', 45, null, 80, 8)
->ln(12)
// 체크박스
->checkboxField('agree', 15, null, 5, 5)
->cell(0, 5, ' I agree to the terms', newLine: true)
->ln(10)
// 드롭다운
->cell(30, 8, 'Country:')
->comboboxField('country', 45, null, 60, 8, ['US', 'UK', 'TW', 'JP', 'DE'])
->ln(12)
// 제출 버튼
->buttonField('submit', 45, null, 40, 10, 'Submit', 'submitForm("https://example.com/submit")');텍스트 및 체크박스 필드
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);$y에 null을 전달하면 현재 수직 커서 위치를 사용합니다.
라디오 버튼 필드
php
$pdf->radioField(string $name, float $x, float $y, float $w, float $h, array $prop = []);같은 $name을 가진 라디오 버튼은 상호 배타적 그룹을 형성합니다:
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');목록, 콤보 박스 및 버튼 필드
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()는 스크롤 가능한 다중 행 목록을 렌더링합니다. comboboxField()는 단일 행 드롭다운을 렌더링합니다. buttonField()는 선택적 JavaScript 액션 문자열이 포함된 푸시 버튼을 생성합니다.
php
$pdf->buttonField('reset', 15, null, 40, 10, 'Reset', 'this.resetForm()')
->buttonField('print', 60, null, 40, 10, 'Print', 'this.print()');필드 속성
$prop 배열은 필드의 외관과 동작을 제어합니다:
| 키 | 유형 | 설명 |
|---|---|---|
border | array | width, color, style 키를 가진 테두리 스타일 |
bgcolor | array | 배경 색상으로 [r, g, b] 형식 |
font | string | 글꼴 패밀리 이름 |
fontSize | float | 포인트 단위의 글꼴 크기 |
alignment | string | 텍스트 정렬: left, center, right |
maxLength | int | 최대 문자 수 (텍스트 필드) |
readonly | bool | 사용자 편집 방지 |
required | bool | 폼 유효성 검사에 필수로 표시 |
value | string | 기본값 / 초기값 |
폼 평탄화
평탄화는 모든 인터랙티브 필드를 정적이고 편집 불가능한 콘텐츠로 변환합니다. 이는 완성된 폼을 보관하거나 최종 읽기 전용 PDF를 생성할 때 유용합니다.
php
$pdf->flattenFields(); // 모든 폼 필드를 정적 콘텐츠로 변환평탄화 후에는 필드 값이 영구적인 텍스트가 됩니다. 더 이상 PDF 뷰어에서 필드를 편집할 수 없습니다.