Creating a PDF (optionally, a flattened / uneditable PDF) from PHP code is super easy when using The PDF Toolkit (or PDFtk for short).
Most web hosting providers already have it installed on their servers, and those that don’t will usually either install it for free or for a small fee. If you need to install PDFtk on your server, you can download the latest version here.
Lets get started on filling in that PDF form with our PHP code…
If you created the PDF form, you should know the names of the fields you created… but if you didn’t create the PDF, or have forgotten - don’t worry, we can make a list of all the fields you can fill in using the following code:
<?php
exec("pdftk test.pdf dump_data_fields > fields.txt");
?>
Lets save the above code as fields.php, and place our PDF form, test.pdf, in the same directory. When we visit out fields.php page in a web browser, you should have a new file in the directory, fields.txt which contains some text that looks like this:
---
FieldType: Text
FieldName: first_name
FieldFlags: 0
FieldJustification: Left
---
FieldType: Text
FieldName: last_name
FieldFlags: 0
FieldJustification: Left
---
FieldType: Text
FieldName: date
FieldFlags: 0
FieldJustification: Center
Note: If your fields.txt file is empty, please make sure that you have PDFtk installed correctly (or contact your web hosting provider)!
So, in this example, our PDF, test.pdf, has 3 textboxes, named first_name, last_name, and date.
Now we need to create a Form Data Format (FDF) file that’s going to hold our field names and field values. Then, we’ll use that specially formatted FDF file to fill in our PDF file and optionally, flatten it so it cannot be edited.
<?php
$fName = 'Ryan';
$lName = 'Kempt';
$dateNow = date('F jS, Y');
$fdf = '%FDF-1.2
1 0 obj<</FDF<< /Fields[
<</T(first_name)/V('.$fName.')>>
<</T(last_name)/V('.$lName.')>>
<</T(date)/V('.$dateNow.')>>
] >> >>
endobj
trailer
<</Root 1 0 R>>
%%EOF';
file_put_contents('test.fdf', $fdf);
exec("pdftk test.pdf fill_form test.fdf output filled.pdf flatten");
?>
Save the above code as create_pdf.php, in the same directory as the rest of the files we’ve created and open it up in a web browser. You should have 2 new files, test.fdf, which defines our field names and field values, and filled.pdf which is a flattened PDF with the variables in it, which is our final product.
Lets break down what’s exactly happening in this code.
First, we define our field values, the values we’re going to place into our PDF. I’ve used some static content, but these values can be anything from data from an HTML form to data pulled from an SQL database.
Next up, we define our FDF content. The only thing you need to edit here are the Fields which are these guys:
<</T(first_name)/V('.$fName.')>>
<</T(last_name)/V('.$lName.')>>
<</T(date)/V('.$dateNow.')>>
The value in the parenthesis after /T is the field name of the field in the PDF, and the value after /V is the value you want to display in that field. You can add or remove as many of these field name / field value lines as you need to.
Then we save the FDF contents to a file named test.fdf.
Lastly, we execute a command that tells PDFtk to fill in our test.pdf file with the field name / field values defined in test.fdf, and to output this filled in form to filled.pdf, and to flatten it so it cannot be edited.