Sie sind auf Seite 1von 5

8/31/2019 How to create a web service to process PDF Form data (FDF)

How to create a web service to process


PDF Form data (FDF)
Alex Lunnon
Oct 20, 2015 · 4 min read

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 ?

Submit PDF Form

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.

What could this look like? Here is an example in Adobe Reader


https://medium.com/@alexlunnon/submit-pdf-form-b6b305e4b245 1/5
8/31/2019 How to create a web service to process PDF Form data (FDF)

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)

escapeshellcmd("pdftk ".$pdf_file." fill_form ".$fdf_file." output


CompletedForms/".$outpdf_file.".pdf flatten")

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.

//Step 1: Get the FDF Raw Data

$FDFData = file_get_contents('php://input');

//Check it to see if it is empty or too short

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)

//Step 2: Create a random number for file name

$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.

$fdfFileName = substr($pdfFileName , 0, strlen($pdfFileName)-4);


//remove .pdf
$fdfFileName = $newFileID;
$fdfFileName = "$fdfFileName.fdf"; //add .fdf
$fdffp = fopen($fdfFileName, "w");
fwrite($fdffp, $FDFData, strlen($FDFData)); //write into a file
fclose($fdffp);

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;

$command = escapeshellcmd("pdftk ".$pdf_file." fill_form


".$fdf_file." output CompletedForms/".$outpdf_file.".pdf flatten");
system("PATH=$PATH:/usr/local/bin/ && $command",$response);
if ($response===FALSE){
//there was an error, handle it
}

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)

//Step 5: Email Completed PDF form


$email_to = "recieving@email.com"; // The email you are sending to
(example)
$email_from = "sending@email.com"; // The email you are sending from
(example)
$email_subject = "Successfully Submitted Form : SubmittedForm.pdf";
// The Subject of the email
$email_txt = "Your form has been successfully submitted. Your
referance code is : ".$outpdf_file; // Message that the email has in
it
$fileatt = "CompletedForms/".$outpdf_file.".pdf"; // Path to the
file (example)
$fileatt_type = "application/pdf"; // File Type
$fileatt_name = "SubmittedForm.pdf"; // Filename that will be used
for the file as the attachment
$file = fopen($fileatt,'rb');
$data = fread($file,filesize($fileatt));
fclose($file);
$semi_rand = md5(time());
$mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
$headers="From: $email_from"; // Who the email is from (example)
$headers .= "nMIME-Version: 1.0n" .
"Content-Type: multipart/mixed;n" .
" boundary="{$mime_boundary}"";
$email_message .= "This is a multi-part message in MIME format.nn" .
"--{$mime_boundary}n" .
"Content-Type:text/html; charset="iso-8859-1"n" .
"Content-Transfer-Encoding: 7bitnn" . $email_txt;
$email_message .= "nn";
$data = chunk_split(base64_encode($data));
$email_message .= "--{$mime_boundary}n" .
"Content-Type: {$fileatt_type};n" .
" name="{$fileatt_name}"n" .
"Content-Transfer-Encoding: base64nn" .
$data . "nn" .
"--{$mime_boundary}--n";

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);

FELLOWSHIP APPLICANT QUALIFICATIONS INQUIRY.pdf


This file is has been configured so that when the submit button is pressed it will submit
the data in the FDF format to a specified address. Most advanced/professional PDF
editors will allow users to define this kind of behavior against a button. You can see
how to do this within Nitro Pro 9 through a short screencast.

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)

Submitting a PDF Form

PHP Programming Pdf

About Help Legal

https://medium.com/@alexlunnon/submit-pdf-form-b6b305e4b245 5/5

Das könnte Ihnen auch gefallen