Zend - The PHP Company




Miscellaneous

Add Code


Populate PDF form fields  

Type: code fragment
Added by: amikeal
Entered: 04/06/2002
Last modified: 06/12/2001
Rating: ***** (3 votes)
Views: 10216
If you want to use PHP to populate fields in a PDF form (for instance, extracting data from a database and prefilling fields for the user) and you don't have the FDF module available to you for whatever reason, this function should work. Regardless, I think this function is more straightforward to use than the FDF module functions.


<?php
/* 
    WARNING!! THIS FUNCTION SENDS HTTP HEADERS! It MUST be called 
    before any content is spooled to the browser, or the function will fail! 
    
    void output_fdf (string $pdf_file, array $pdf_data)
    
        $pdf_file:     a string containing a URL path to a PDF file on the server.
                    This PDF MUST exist and contain fields with the names 
                    referenced by $pdf_data for this function to work.
        $pdf_data:    an array of any fields in $pdf_file that you want to populate,
                    of the form key=>val; where the field name is the key, and
                    the field's value is in val.
    
*/    

function output_fdf ($pdf_file$pdf_data) {
    
    
$fdf "%FDF-1.2n%����n";
    
$fdf .= "1 0 obj n<< /FDF ";
    
$fdf .= "<< /Fields [n"
    
    foreach (
$pdf_data as $key => $val)
        
$fdf .= "<< /V ($val)/T ($key) >> n";
        
    
$fdf .= "]n";
    
$fdf .= "/F ($pdf_file) >>";
    
$fdf .= ">>nendobjntrailern<<n/Root 1 0 Rnn>>n";
    
$fdf .= "%%EOF";
    
    
/* Now we display the FDF data which causes Acrobat to start  */
    
    
header ("Content-Type: application/vnd.fdf");
    print 
$fdf;
    
}

?>


Usage Example


$file = 'http://url.com/form.pdf';
$data = array (
   'field1' => 'value',
   'field2' => 'value'
);

output_fdf($file, $data);


Rate This Script





Search



This Category All Categories