0

I am building an application to help assist in setting up PDF forms for my site. I have standard forms that I use, but need to change a few hidden fields in the PDF file each time I add the standard form to a new account.

Using PHP how can I update the value of a field in a PDF file, then save the file? I am assuming I need to use some sort of PHP-PDF library, so a FREE one would be helpful.

(The forms are already programmed with form fields using Adobe Acrobat and have unique field names. All I need to do is update a couple of the existing fields using the field name as a key)

Example-

PDF File Location = www.mysite.com/accounts/john_smith/form.pdf
PDF Field to update (Field Name) = "account_directory"
PDF Field value to be set = "john_smith"
Charles
  • 50,943
  • 13
  • 104
  • 142
JimmyJammed
  • 9,598
  • 19
  • 79
  • 146
  • possible duplicate of [PDF Editing in PHP?](http://stackoverflow.com/questions/7364/pdf-editing-in-php) – Marc B Jun 18 '13 at 21:14
  • Not quite. I am not trying to change text on a PDF file, but set a value of a form field on the PDF. – JimmyJammed Jun 18 '13 at 21:31

1 Answers1

0

This is clunky but it gets the job done: Use createXFDF.php and then use PDFTK twice. Make sure your template pdf has extended features in Adobe Reader. Create an XFDF file with the data you will use to populate the PDF - remember that field names in pdf must match those in the xfdf. The data is an array of field name, field value. Use PDFTK with your template pdf and the XFDF to create a new intermediate PDF. Do NOT use FLATTEN - that will remove editability. Now use PDFTK cat with your new intermediate PDF to create your final PDF - it somehow gets rid of the Adobe digital signature (Keep a pdf form editable after filling it with pdftk - see Marco's answer).

Sample code:

$data = array();
$field_name = "account_directory";
$field_value = "john_smith";
$data[$field_name] = $field_value;
/* Make $data as big as you need with as many field names/values 
as you need to populate your pdf */

$pdf_template_url = 'http://yoursite.com/yourpath/yourtemplate.pdf';
include 'createXFDF.php';
$xfdf = createXFDF( $pdf_template_url, $data );

/* Set up the temporary file name for xfdf */
$filename = "temp_file.xfdf";

/* Create and write the XFDF for this application */
$directory = $_SERVER['DOCUMENT_ROOT']."/path_to_temp_files/";
$xfdf_file_path = $directory.$filename ; /* needed for PDFTK */
// Open the temp xfdf file and erase the contents if any
$fp = fopen($directory.$filename, "w");
// Write the data to the file
fwrite($fp, $xfdf);
// Close the xfdf file
fclose($fp); 

/*  Write the pdf for this application - Temporary, then Final */
$pdf_template_path = '/yourpath/yourtemplate.pdf';
$pdftk = '/path/to/pdftk'; /* location of PDFTK */
$temp_pdf_file_path = substr( $xfdf_file_path, 0, -4 ) . 'pdf'; 

$command = "$pdftk $pdf_template_path fill_form $xfdf_file_path output $temp_pdf_file_path"; 
/* Note that the created file is NOT flattened so that recipient
can edit form - but this is not enough to allow edit with
Adobe Reader: will also need to remove the signature with PDFTK cat */

exec( $command, $output, $ret );

/* Workaround to get editable final pdf */ 
$pdf_path_final = $directory. "your_final_filename.pdf" ;
$command2 = "$pdftk $temp_pdf_file_path cat output $pdf_path_final";
exec( $command2, $output2, $ret2 );
/* Your pdf is now saved 

/* Remember to UNLINK any files you don't want to save - the .xfdf and the temporary pdf */
Community
  • 1
  • 1
Leslie
  • 1
  • 1