Introduction
Fillable PDFs allow users to input data directly into a document, making them ideal for applications, contracts, surveys, and registration forms. Instead of static PDFs, fillable PDFs let users type, check, or sign fields interactively.
With PHP, we can:
✅ Generate PDF forms dynamically
✅ Add text fields, checkboxes, and dropdowns
✅ Extract user-submitted form data from PDFs
✅ Populate PDFs with database data
In this guide, we’ll use TCPDF to create fillable forms and FPDM to pre-fill and extract data from PDFs. 🚀
1. Installing TCPDF and FPDM for PDF Forms
Install via Composer (Recommended)
composer require tecnickcom/tcpdf
composer require smalot/fpdm
Include TCPDF and FPDM in your PHP script:
require 'vendor/autoload.php';
use TCPDF;
use FPDM;
✅ Now, PHP is ready to generate and process fillable PDFs.
2. Creating a Basic Fillable PDF Form
Example: Generate a PDF with Text Input Fields
$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
// Add Text Field
$pdf->TextField('name', 50, 10, ['border' => 1]);
$pdf->Text(20, 30, 'Name:');
$pdf->SetXY(60, 25);
$pdf->Cell(50, 10, '', 1);
// Add Email Field
$pdf->TextField('email', 50, 10, ['border' => 1]);
$pdf->Text(20, 50, 'Email:');
$pdf->SetXY(60, 45);
$pdf->Cell(50, 10, '', 1);
// Output Form PDF
$pdf->Output('fillable_form.pdf', 'F');
echo "Fillable PDF form created!";
Explanation:
✅ TextField('name', width, height, options)
– Creates a text input field.
✅ Text labels (Text(x, y, 'label')
) – Adds descriptions.
✅ Users can type directly into the fields using a PDF viewer.
🔹 Now, the generated PDF can accept user input!
3. Adding Checkboxes and Dropdowns to a PDF Form
Forms often include checkboxes, radio buttons, and dropdowns for user input.
Example: Add Checkboxes and Dropdowns
$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
// Add Checkbox
$pdf->CheckBox('agree_terms', 5, true);
$pdf->Text(20, 30, 'Agree to Terms & Conditions');
// Add Dropdown
$pdf->ComboBox('gender', 30, ['Male', 'Female', 'Other'], ['border' => 1]);
$pdf->Text(20, 50, 'Gender:');
// Output PDF Form
$pdf->Output('interactive_form.pdf', 'F');
echo "Interactive PDF form created!";
Explanation:
✅ Checkbox (CheckBox('name', size)
) – Adds a clickable checkbox.
✅ Dropdown (ComboBox('name', width, options)
) – Provides a selection list.
🔹 Users can now select options in the PDF form!
4. Extracting Submitted Data from a PDF Form
Once a user fills out a PDF form, we need to extract the submitted data.
Example: Extract Data from a Filled PDF
$filledPdf = 'filled_form.pdf';
$fpdm = new FPDM($filledPdf);
$data = $fpdm->getData();
print_r($data);
Explanation:
✅ Extracts form fields filled by the user.
✅ Useful for processing user-submitted PDFs.
🔹 Now, form responses can be stored in a database!
5. Pre-Filling a PDF Form with Database Data
Sometimes, we need to pre-fill PDF forms with existing user data.
Example: Populate a PDF Form with User Data
$fields = ['name' => 'John Doe', 'email' => 'john@example.com'];
$pdf = new FPDM('template.pdf');
$pdf->Load($fields);
$pdf->Merge();
$pdf->Output('filled_form.pdf', 'F');
echo "Pre-filled form generated!";
Explanation:
✅ Populates the form fields (name
, email
) automatically.
✅ Useful for auto-filling invoices, contracts, and reports.
🔹 The user can edit and review pre-filled data before submitting.
6. Adding Digital Signatures to PDF Forms
To make PDF forms legally binding, add a digital signature field.
Example: Add a Signature Field
$pdf = new TCPDF();
$pdf->AddPage();
$pdf->SetFont('helvetica', '', 12);
// Signature Field
$pdf->TextField('signature', 50, 10, ['border' => 1]);
$pdf->Text(20, 80, 'Signature:');
$pdf->SetXY(60, 75);
$pdf->Cell(50, 10, '', 1);
$pdf->Output('signature_form.pdf', 'F');
echo "PDF form with signature field created!";
How It Works:
✔ Users can sign electronically in Adobe Acrobat.
✔ Signature validation prevents document tampering.
🔹 Great for contracts and legal forms!
7. Automatically Process and Save Submitted PDF Forms
To automate form processing, save user-submitted data into a database.
Example: Save PDF Form Data to MySQL
$conn = new mysqli("localhost", "root", "", "pdf_forms");
$filledPdf = 'filled_form.pdf';
$fpdm = new FPDM($filledPdf);
$data = $fpdm->getData();
$stmt = $conn->prepare("INSERT INTO form_responses (name, email) VALUES (?, ?)");
$stmt->bind_param("ss", $data['name'], $data['email']);
$stmt->execute();
echo "Form data saved successfully!";
✅ Automates form submission handling.
✅ Stores responses in a structured database.
8. Validating Form Data Before Processing
Ensure valid data before processing user-submitted forms.
Example: Validate Form Fields Before Saving
if (empty($data['name']) || !filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
die("Invalid data submitted!");
}
✅ Prevents missing or incorrect data from being processed.
9. Emailing a Completed PDF Form as an Attachment
Once a user fills out a PDF, send it as an email attachment.
Example: Email PDF Form
use PHPMailer\PHPMailer\PHPMailer;
$mail = new PHPMailer();
$mail->addAttachment('filled_form.pdf');
$mail->send();
echo "PDF form sent via email!";
✅ Great for form submissions that require email confirmation.
Best Practices for PDF Forms in PHP
✔ Use fillable fields (TextField
, CheckBox
, ComboBox
) for user input.
✔ Extract form data to store responses in a database.
✔ Pre-fill forms for personalized user experience.
✔ Validate form data before processing.
✔ Add digital signatures for legally binding forms.
Conclusion
With TCPDF and FPDM, you can create interactive PDF forms in PHP that allow text input, selections, and signatures.
✅ Generate fillable PDF forms dynamically.
✅ Extract submitted data for processing.
✅ Pre-fill forms with user details from a database.
✅ Automate form handling for seamless workflows.
By implementing these techniques, you can build interactive, secure, and efficient PDF forms in PHP! 🚀