Generate a Filled PDF Form File
This has got to be the most requested thing relating to my article since I posted it many years ago. No matter how many times I state that the programming I put together does not create actual PDF files, I still the question of how to do it. Don't worry, I have you covered! As a word of caution, this doesn't work on all servers, any may not work on many of the larger hosting companies due to restrictions on their systems. Those of you that are successful with this in a shared hosting environment should let me know about it so I can help point people in the correct direction!
To accomplish our goals, we will be using PDF Lab's PDFtk on the web server. The first thing that will need to be done is to get this installed on the server so that you can utilize it from your script. I won't go into detail on how to install the software on your server as that is outside the scope of this article. If you are on a shared host, you may need to ask your provider if the software is available or if it can be made available.
Another important thing that is needed is the ability to use the PHP function
exec(). This will be used to call the pdftk system command to create the final version of the PDF file that contains all the user-submitted data filled in. Be sure to review the warnings on the function page linked to help prevent headaches while attempting to debug problems!
Processing the User-Submitted Data
For the sake of simplicity, I won't go over how to process the data submitted from the user from start to finish. Instead, I will assume that the following has been completed:
- A form submits data to the processing script
- There is a PDF template saved on the server to use the submitted data with
The processing script generates the XFDF string with the
createXFDF()function and data is saved to the
The contents of the
$xfdfvariable have been written to a file on the server's filesystem
Take the time to be sure that the above statements are true about your project to make things easier to implement and test before we get into the last piece. If you haven't already processed the user-submitted data or saved the PDF form data to a file, please review those articles before continuing.
Creating the PDF Filled With User Data
Are you ready for this final step yet? Yeah... I figured you had skipped down here already. After following the steps from above, you have the system path of the user-submitted data file (
$xfdf_file_path) and the system path to the PHP-writeable result directory (
$result_directory). There are a couple other pieces we will need for this to work properly:
$pdf_template_path = dirname( __FILE__ ) . '/pdf_form_template.pdf'; $pdftk = '/usr/bin/pdftk'; $pdf_name = substr( $xfdf_file_path, 0, -4 ) . 'pdf'; $command = "$pdftk $pdf_template_path fill_form $xfdf_file_path output $pdf_name flatten";
What we have above is the core of this step in the process. In order, we set the location of our template PDF that will be filled, set the system path for the pdftk executable, generate a name for the resulting PDF file, generate the system path for that same file, and finally create the system command that will be used to create the final piece. At this point, it is good no mention that the "flatten" part of the command is optional. In a nutshell, what it does is remove the fields and replaces them with the data. If you want the fields to remain there and just have the data entered, remove the "flatten" option from the
The last step is to simply call the command to produce the file:
exec( $command, $output, $ret );
If everything has gone well, you will now have a new PDF file based on your template file that has all the user-submitted (and processed) data filled into it and saved in your result directory!
Suggestions or Improvements?
Do you have something that may benefit others regarding what you find on this page? Would you like to share it? If so, simply contact me and I will review it and possibly post it here.
Did this article help you save time for a project? Did this information help you create your website or web application? If so, please send a donation my way - no amount is too small to show your appreciation.