
Today, PDF files have become one of the most common methods of sharing documents online. Using a PDF document is often the foremost option, whether you need to provide your clients’ documents to third-party service providers like insurance companies or banks, or simply need to send a CV to an employer. Working with PDF files allows you to share plain as well as formatted text, hyperlinks, images, and even different fillable forms that you need.
In this blog, we will understand how one can fill out PDF forms using PHP and a tremendous PDF manipulation tool known as PDFtk Server. Here we will be demonstrating how one can work dynamically by adding data in PDF form through Html forms.
A fillable PDF is a PDF document that contains some fields that are editable without PDF editor software. To fill the fillable PDF dynamically, one has to create an HTML form and dynamically bind it. To accomplish this, we will create a PHP script.
Below are the following steps and some software & libraries to fill a fillable PDF.
Prerequisites:
PDFtk Server – PDF manipulation software
Composer – Composer
php-pdftk Library for PDFtk
Go to the above PDFtk server Link and install the PDFtk server according to your operating system.

After installing the software, open the command prompt and run the Command:

Create a folder for the script on your local server; the folder name can be anything. In this case, we have created a folder named “fillable-pdf”. Now, create index.php in the fillable-pdf folder.
www/public_html/ fillabe-pdf/index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>How To Fill A Fillable PDF Form with PHP using PDFtk</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="container">
<form action="handler.php" method="POST">
<h1>Generate Fillable PDF Dynamically</h1>
<p>Fill the form and your data will fill in a PDF</p>
<div class="form-group row">
<div class="col-lg-6">
<input type="text" name="fname" class="form-control" placeholder="First Name">
</div>
<div class="col-lg-6">
<input type="text" name="lname" class="form-control" placeholder="Last Name" required>
</div>
</div>
<div class="form-group row">
<div class="col-lg-6">
<input type="email" name="email" class="form-control" placeholder="Email" required>
</div>
<div class="col-lg-6">
<input type="tel" name="phone" class="form-control" placeholder="Phone" required>
</div>
</div>
<div class="form-group">
<textarea name="message" cols="30" rows="10" class="form-control" placeholder="Your Message" required></textarea>
</div>
<button type="submit" class="btn btn-block btn-success">Sumbit</button>
</form>
</div>
</body>
</html>
Create a style.css file for the form to look good. www/public_html/ fillabe-pdf/style.css
body {
padding: 5rem 0;
}
.btn-block {
display: block;
width: 10%;
}
.btn-success {
color: #fff;
background-color: #0062cc;
border-color: #007bff;
}
.btn-success:hover {
color: #fff;
background-color: #0062cc;
border-color: #0062cc;
}

Create a composer.json file for installing the PDFtk library.
www/public_html/ fillabe-pdf/composer.json
{
"name": "mrdigital/pdftk",
"autoload" : {
"psr-4" : {
"Classes\\":"./classes"
}
},
"require": {
"mikehaertl/php-pdftk": "^0.10.3"
}
}
After this, run this command:
Composer requires mikehaertl/php-pdftk; this command creates a vendor folder and downloads the PDFtk library.
Check the sample fillable PDF file fields and map fields with the handler.php file. Then open a fillable PDF on any editor software to check input fields with mapped fields names. You can use this https://www.pdffiller.com/ site to get the field’s name.
name.

Create a handler file to handle post requests, mapping pdf fields' value to post form value.
www/public_html/ fillabe-pdf/handler.php
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
exit;
}
define('ACCESSCHECK', TRUE);
require_once 'vendor/autoload.php';
use Classes\GeneratePDF;
$data = [
'name_field' => $_POST['fname'] .' ' . $_POST['lname'],
'email_field' => $_POST['email'],
'phone_field' => $_POST['phone'],
'enquiry_field' => $_POST['message']
];
$pdf = new GeneratePdf;
$response = $pdf->generate($data);
print_r($response);
Create Folder Classes for Create class GenratePDF.
www/public_html/ fillabe-pdf/classes/GeneratePDF.php
<?php
namespace Classes;
if(!defined('ACCESSCHECK')) {
die('Direct access not permitted');
}
use mikehaertl\pdftk\Pdf;
class GeneratePDF {
public function generate($data)
{
try {
$filename = 'pdf_fillabel' . rand() . '.pdf';
$pdf = new Pdf('./test.pdf');
$pdf->fillForm($data)
->flatten()
->saveAs( './pdf_fill/' . $filename);
return $filename;
}
catch(Exception $e)
{
return $e->getMessage();
}
}
}
Create a pdf_fill folder for storing fillable pdf.
Protect your folder for security reasons, create an empty index.php file in the classes and pdf_fill folder.
www/public_html/ fillabe-pdf/classes/index.php or www/public_html/ fillabe-pdf/pdf_fill/index.php
<?php
// Nothing here
With the above implementation, you can install PDFtk and learn some of its useful commands like dump_data_fields and fill_form. With all the above steps, you will be able to successfully achieve the PDF fillable custom functionality.
Please note that this implementation is basic, and we have tried to keep things as simple as possible. If still, you are still having some problems, you can connect with our development team for any further assistance.
Q: What is PDFtk Server, and why is it used in PHP for PDF manipulation?
A: PDFtk Server is a powerful solution that is used to work with PDFs. Moreover, it is used in PHP with libraries like php-pdftk to merge, split, fill forms, and manipulate PDFs.
Q: Can I fill PDF forms dynamically using PHP without PDFtk?
A: Yes, you can use alternative libraries like TCPDF, FPDF, or FPDI to fill PDF forms dynamically using PHP without PDFtk.
Q: How do I get the field names of a fillable PDF form?
A: To get the field names of a fillable PDF form, you can use the dump_data_files command or use a PDFfiller (an online PDF editor).
Q: Is it possible to automate filling multiple PDFs with the same data using PHP?
A: Yes, it is possible to automate filling multiple PDFs with the same data using PHP.
We are more than just developers and consultants—we are your partners in navigating the digital landscape. Let us be the engine behind your next big success while you focus on your core vision.
Explore Opportunities!