How To Create A Web Service To Process PDF Form Data (FDF)
How To Create A Web Service To Process PDF Form Data (FDF)
How can we capture PDF data users enter? Will all our users be able to open the PDF?
what could a ‘PDF’ solution look like ?
One possible solution can be submitting your users PDF form data to a server, this
allows you to process the data immediately, email a customer with a confirmation or
write data directly to a database. This method is largely supported by all major desktop
viewers however is typically unsupported on mobile and web platforms.
Pre-requisites
For this example we will need to install a tool, PDFtk Server, this will allow us to run
some PDF specific commands from a server side PHP script. For example when you
look to Step 4 in the code below you can see the command (Windows + Mac Support)
This command allows us to take our submitted FDF data and apply it back into a PDF
file which we will attach to a confirmation email.
Getting Started
Head over to github to download this repository, the main file which will process our
form data is : epaformsubmittal.php
This file is the end point that will be targeted by your PDF, this could either be a single
end point for all forms or in this case its simply the end point that will be used for the
example EPA Form we will be configuring to submit data. Step 1. The first thing we do
in this process is collect the FDF data that has been submitted, we conduct a quick
check to make sure the string length is greater than 10 and continue onwards.
$FDFData = file_get_contents('php://input');
if ( strlen($FDFData)<10)
{
header("Location: http://www.pdfill.com/pdf_action.html#4");
exit;
}
Step 2. In this case all files are going to use a randomized number, obviously this is
something that should be changed and a specific receipt number or customer number
should ideally be used.
https://medium.com/@alexlunnon/submit-pdf-form-b6b305e4b245 2/5
8/31/2019 How to create a web service to process PDF Form data (FDF)
$newFileID = GetRandonFolerName();
Step 3. Here we go ahead and write the FDF file to disk based on what has been
submitted, we will use our randomized file name from step 3.
Step 4. In this step we are going to use the command line from the PDFtk tool to merge
the FDF data back into a blank server side PDF, we are going to do this so we can then
go ahead and attach that PDF to a confirmation email. We could for example merge the
data back into a receipt form which included a receipt number and processing time. To
keep it simple however this is the same form used to submit (without a submittal
button).
$fdf_file = $fdfFileName;
$pdf_file = "blankform.pdf";
$outpdf_file= $newFileID;
Step 5. In this step we go ahead and send an email to a pre-defined email address with
the attached PDF from Step 4. In this example we may want to instead send it to an
email address in the form data or process it into a database. We also go ahead and send
a FDF file back to the client which includes a javascript pop up message to confirm
receipt. This message could be tailored depending on the specific workflow and
outcomes.
https://medium.com/@alexlunnon/submit-pdf-form-b6b305e4b245 3/5
8/31/2019 How to create a web service to process PDF Form data (FDF)
mail($email_to,$email_subject,$email_message,$headers);
//Send Success Response to PDF Reader as FDF Type and clean up FDF
file
header('Content-type: application/vnd.fdf');
readfile('success.fdf');
unlink($fdf_file);
https://medium.com/@alexlunnon/submit-pdf-form-b6b305e4b245 4/5
8/31/2019 How to create a web service to process PDF Form data (FDF)
https://medium.com/@alexlunnon/submit-pdf-form-b6b305e4b245 5/5