跳轉到

互動式表單範例

需求套件nextpdf/core 難度:中級

完整程式碼

<?php

declare(strict_types=1);

use NextPDF\Core\Document;
use NextPDF\Core\Form\Fields\TextField;
use NextPDF\Core\Form\Fields\CheckboxField;
use NextPDF\Core\Form\Fields\ComboBoxField;
use NextPDF\Core\Form\Fields\RadioGroup;
use NextPDF\Core\Form\Fields\RadioOption;
use NextPDF\Core\Form\Fields\SignatureField;
use NextPDF\Core\Form\ValueObjects\FieldAppearance;
use NextPDF\Core\ValueObjects\PageSize;
use NextPDF\Core\ValueObjects\Margin;
use NextPDF\Core\ValueObjects\Position;
use NextPDF\Core\ValueObjects\Rectangle;
use NextPDF\Core\ValueObjects\Color;

// ─── 文件初始化 ───────────────────────────────────────────────
$document = Document::createStandalone(
    pageSize: PageSize::A4,
    margin:   Margin::symmetric(vertical: 20.0, horizontal: 25.0),
);

$page    = $document->pages()->add();
$text    = $document->text();
$drawing = $document->drawing();
$forms   = $document->forms();

// ─── 共用欄位外觀設定 ─────────────────────────────────────────
$fieldAppearance = FieldAppearance::create()
    ->withBackgroundColor('#F9FAFB')
    ->withBorderColor('#D1D5DB')
    ->withBorderWidth(0.5)
    ->withFontName('NotoSansCJKtc')
    ->withFontSize(10.0)
    ->withTextColor('#111827');

// ─── 標題 ─────────────────────────────────────────────────────
$drawing->rectangle(
    x: 25.0, y: 20.0, width: 160.0, height: 15.0,
    fill: Color::fromHex('#1E3A8A'),
    cornerRadius: 2.0,
);
$text->write(
    text:      '客戶服務申請表',
    position:  Position::at(x: 105.0, y: 25.0),
    fontSize:  14.0,
    fontName:  'NotoSansCJKtc-Bold',
    color:     Color::fromHex('#FFFFFF'),
    alignment: 'center',
    maxWidth:  160.0,
);

// ─── 基本資料區塊 ─────────────────────────────────────────────
$text->write(
    text:     '壹、基本資料',
    position: Position::at(x: 25.0, y: 44.0),
    fontSize: 11.0,
    fontName: 'NotoSansCJKtc-Bold',
    color:    Color::fromHex('#1E3A8A'),
);
$drawing->line(
    x1: 25.0, y1: 51.0, x2: 185.0, y2: 51.0,
    color: Color::fromHex('#BFDBFE'), width: 0.5,
);

// 姓名欄位
$text->write(
    text:     '姓名 *',
    position: Position::at(x: 25.0, y: 57.0),
    fontSize: 9.5,
    fontName: 'NotoSansCJKtc',
    color:    Color::fromHex('#374151'),
);
$forms->addTextField(
    field:  TextField::create(
                name:        'full_name',
                label:       '姓名',
                placeholder: '請輸入您的全名',
                required:    true,
                maxLength:   50,
                tooltip:     '請輸入您的完整姓名',
            )->withAppearance($fieldAppearance),
    bounds: Rectangle::create(x: 25.0, y: 62.0, width: 80.0, height: 10.0),
);

// 電子郵件欄位
$text->write(
    text:     '電子郵件 *',
    position: Position::at(x: 115.0, y: 57.0),
    fontSize: 9.5,
    fontName: 'NotoSansCJKtc',
    color:    Color::fromHex('#374151'),
);
$forms->addTextField(
    field:  TextField::create(
                name:        'email',
                label:       '電子郵件',
                placeholder: 'example@domain.com',
                required:    true,
                maxLength:   100,
            )->withAppearance($fieldAppearance),
    bounds: Rectangle::create(x: 115.0, y: 62.0, width: 70.0, height: 10.0),
);

// 電話欄位
$text->write(
    text:     '聯絡電話',
    position: Position::at(x: 25.0, y: 78.0),
    fontSize: 9.5,
    fontName: 'NotoSansCJKtc',
    color:    Color::fromHex('#374151'),
);
$forms->addTextField(
    field:  TextField::create(
                name:        'phone',
                label:       '電話',
                placeholder: '(02) xxxx-xxxx',
                required:    false,
                maxLength:   20,
            )->withAppearance($fieldAppearance),
    bounds: Rectangle::create(x: 25.0, y: 83.0, width: 60.0, height: 10.0),
);

// 縣市下拉
$text->write(
    text:     '所在縣市 *',
    position: Position::at(x: 95.0, y: 78.0),
    fontSize: 9.5,
    fontName: 'NotoSansCJKtc',
    color:    Color::fromHex('#374151'),
);
$forms->addComboBox(
    field:  ComboBoxField::create(
                name:     'city',
                label:    '縣市',
                options:  [
                    ''   => '請選擇',
                    'tp' => '台北市',
                    'nt' => '新北市',
                    'tc' => '台中市',
                    'kh' => '高雄市',
                    'other' => '其他',
                ],
                selected: '',
                required: true,
            )->withAppearance($fieldAppearance),
    bounds: Rectangle::create(x: 95.0, y: 83.0, width: 50.0, height: 10.0),
);

// ─── 服務類型區塊 ─────────────────────────────────────────────
$text->write(
    text:     '貳、申請服務類型',
    position: Position::at(x: 25.0, y: 101.0),
    fontSize: 11.0,
    fontName: 'NotoSansCJKtc-Bold',
    color:    Color::fromHex('#1E3A8A'),
);
$drawing->line(
    x1: 25.0, y1: 108.0, x2: 185.0, y2: 108.0,
    color: Color::fromHex('#BFDBFE'), width: 0.5,
);

$forms->addRadioGroup(
    group: RadioGroup::create(
        name:    'service_type',
        options: [
            RadioOption::create(value: 'new',      label: '全新申請'),
            RadioOption::create(value: 'upgrade',  label: '方案升級'),
            RadioOption::create(value: 'renew',    label: '續約展延'),
            RadioOption::create(value: 'cancel',   label: '取消服務'),
        ],
        selected: 'new',
        required: true,
    ),
    positions: [
        Rectangle::create(x: 25.0, y: 112.0, width: 5.5, height: 5.5),
        Rectangle::create(x: 65.0, y: 112.0, width: 5.5, height: 5.5),
        Rectangle::create(x: 105.0, y: 112.0, width: 5.5, height: 5.5),
        Rectangle::create(x: 145.0, y: 112.0, width: 5.5, height: 5.5),
    ],
);

// ─── 申請說明區塊 ─────────────────────────────────────────────
$text->write(
    text:     '參、申請說明',
    position: Position::at(x: 25.0, y: 128.0),
    fontSize: 11.0,
    fontName: 'NotoSansCJKtc-Bold',
    color:    Color::fromHex('#1E3A8A'),
);
$drawing->line(
    x1: 25.0, y1: 135.0, x2: 185.0, y2: 135.0,
    color: Color::fromHex('#BFDBFE'), width: 0.5,
);

$forms->addTextField(
    field:  TextField::create(
                name:       'description',
                label:      '申請說明',
                multiline:  true,
                placeholder:'請詳細描述您的申請需求...',
                required:   false,
                maxLength:  500,
            )->withAppearance($fieldAppearance),
    bounds: Rectangle::create(x: 25.0, y: 138.0, width: 160.0, height: 35.0),
);

// ─── 同意條款 ─────────────────────────────────────────────────
$forms->addCheckbox(
    field:  CheckboxField::create(
                name:     'agree_privacy',
                label:    '同意隱私政策',
                checked:  false,
                required: true,
                tooltip:  '勾選表示您同意我們的隱私政策與服務條款',
            ),
    bounds: Rectangle::create(x: 25.0, y: 182.0, width: 5.5, height: 5.5),
);
$text->write(
    text:     '我已閱讀並同意《服務條款》與《隱私政策》',
    position: Position::at(x: 33.0, y: 183.5),
    fontSize: 9.0,
    fontName: 'NotoSansCJKtc',
    color:    Color::fromHex('#374151'),
);

// ─── 數位簽章 ─────────────────────────────────────────────────
$text->write(
    text:     '申請人簽名',
    position: Position::at(x: 25.0, y: 197.0),
    fontSize: 9.5,
    fontName: 'NotoSansCJKtc',
    color:    Color::fromHex('#374151'),
);
$forms->addSignatureField(
    field:  SignatureField::create(
                name:  'applicant_signature',
                label: '申請人簽名',
            ),
    bounds: Rectangle::create(x: 25.0, y: 200.0, width: 80.0, height: 25.0),
);

$text->write(
    text:     '申請日期',
    position: Position::at(x: 120.0, y: 197.0),
    fontSize: 9.5,
    fontName: 'NotoSansCJKtc',
    color:    Color::fromHex('#374151'),
);
$forms->addTextField(
    field:  TextField::create(
                name:        'application_date',
                label:       '申請日期',
                placeholder: 'YYYY-MM-DD',
            )->withAppearance($fieldAppearance),
    bounds: Rectangle::create(x: 120.0, y: 200.0, width: 65.0, height: 10.0),
);

// 必填說明
$text->write(
    text:     '* 標記為必填欄位',
    position: Position::at(x: 25.0, y: 235.0),
    fontSize: 8.0,
    fontName: 'NotoSansCJKtc',
    color:    Color::fromHex('#9CA3AF'),
);

// ─── 輸出 ────────────────────────────────────────────────────
$document->save('/output/service-application-form.pdf');

程式碼說明

延伸閱讀