/** *Generates a form definition file (fdf) * *@note error message is dumped into syslog if supported *@todo Expand support not only to fdf_data_strings (I don't need this feature) *@param String $pdf_url *@param Array $pdf_data the array that holds fields datas (field_name => field_value *@param String $output_mode * 'D' : WARNING!! By default, THIS FUNCTION SENDS HTTP HEADERS! It MUST be called before * any content is spooled to the browser, or the function will fail! * 'S' : Return the fdf file generated as a string * <fdf_file> fullpathname to where the fdf file content has to be saved. *@return mixed ret the return value which can be: * -a boolean true when output_mode is set to 'D' * -a text the fdf content when output_mode is set to 'S' * -an array holding success flag with either the fdf size or the error message **/ function output_fdf($pdf_url, $pdf_data, $output_mode = 'D') { //--------------------------------------------------------- // Ensures pdf path is absolute $pdf_form_url = resolve_pdf_url($pdf_url); // string data, used for text fields, combo boxes and list boxes $fdf_data_strings = $pdf_data; // name data, used for checkboxes and radio buttons // (e.g., /Yes and /Off for true and false) $fdf_data_names = array(); //fields security and accessibility attributes $fields_hidden = array(); $fields_readonly = array(); $fdf = forge_fdf($pdf_form_url, $fdf_data_strings, $fdf_data_names, $fields_hidden, $fields_readonly); switch ($output_mode) { case "D": //Send the fdf header so acrobat recognize it. header("Content-Type: application/vnd.fdf"); print $fdf; $ret = true; break; case "S": //String $ret = $fdf; break; default: // write the file out $error_fdf_access = ''; $fdf_file = $output_mode; $fdf_dir = dirname($fdf_file); //Paranoïd access mode with syslog in background as watchdog for errors if (file_exists($fdf_dir)) { if (is_writable($fdf_dir)) { if (!is_writable($fdf_file) && false) { //Create $error_fdf_access = "can not write fdf file ({$fdf_file}), disk full or missing rights?"; } } else { $error_fdf_access = "can not write into fdf's directory ({$fdf_dir})"; } } else { $error_fdf_access = "can not access to fdf's directory ({$fdf_dir})"; } $success = false; if ($error_fdf_access != "") { $err = "output_fdf : Unable to create fdf file '" . $fdf_file . "'<br> because {$error_fdf_access}."; } else { if ($fp = fopen($fdf_file, 'w')) { $err = fwrite($fp, $fdf, strlen($fdf)); if (function_exists('syslog')) { syslog(LOG_WARNING, "FDF file '" . $output_mode . "' written successfully ({$err} bytes)"); } $success = true; } else { $err = "output_fdf : Unable to generate file '" . $output_mode . "', disk full or corrupted?."; } fclose($fp); } $ret = array("success" => $success, "return" => $err); } return $ret; }
function invoiceToFDF($invoice, $extra_vars = array()) { $fdf_array = array('CompanyName' => $invoice['Customer']['company_name'], 'DateOfSupply' => gmstrftime('%d %b %Y', strtotime($invoice['Invoice']['created'])), 'InvoiceNo' => $invoice['Invoice']['reference'], 'VATTotal' => $this->getVatTotal($invoice), 'SubTotal' => $this->getSubTotal($invoice), 'GrandTotal' => $this->getGrandTotal($invoice)); $address = $invoice['Customer']['address']; $address .= !empty($invoice['Customer']['town']) ? "\n{$invoice['Customer']['town']}" : ''; $address .= !empty($invoice['Customer']['county']) ? "\n{$invoice['Customer']['county']}" : ''; $address .= !empty($invoice['Customer']['post_code']) ? "\n{$invoice['Customer']['post_code']}" : ''; if (!empty($invoice['Customer']['country']) && $invoice['Customer']['country'] != 'UK') { $address .= "\n{$invoice['Customer']['country']}"; } $fdf_array['CompanyAddress'] = $address; if (!empty($extra_vars)) { foreach ($extra_vars as $var_key => $var_val) { $fdf_array[$var_key] = $var_val; } } //ServiceList, ServicePrices $service_list = array(); $service_prices = array(); $breakdown = explode("\n", $invoice['Invoice']['description']); if (!empty($breakdown)) { foreach ($breakdown as $breakdown_item) { if (preg_match('/,/', $breakdown_item)) { list($service_list[], $service_prices[]) = explode(",", $breakdown_item); } else { $service_list[] = $breakdown_item; $service_prices[] = ''; } } $fdf_array['ServiceList'] = implode("\n", $service_list); $fdf_array['ServicePrices'] = implode("\n", $service_prices); } foreach ($fdf_array as $i => $fdfa) { $fdf_array[$i] = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $fdfa); } $fdf_data_names = array(); $fields_hidden = array(); $fields_readonly = array(); $forge_fdf = forge_fdf('', $fdf_array, $fdf_data_names, $fields_hidden, $fields_readonly); return $forge_fdf; }
$_4ur_data_strings['Fall'] = '15'; $_4ur_data_strings['Subject Code'] = 'CSCI'; $_4ur_data_strings['Print Instructors Name'] = 'Goldschmidt, David'; $_4ur_data_strings['specific role of student in the project 1'] = 'Please see attached'; $_4ur_data_strings['indicate expected weekly time commitments 1'] = 'Please see attached'; $_4ur_data_strings['determined 1'] = 'Please see attached'; //Split name by last space to turn "Jane Doe" to "Doe, Jane" as required by the name field of the 4UR. //http://stackoverflow.com/a/1530902/1122135 list($_4ur_first_name, $_4ur_last_name) = preg_split("/\\s+(?=\\S*+\$)/", $_POST['name']); $_4ur_data_strings['Name'] = $_4ur_last_name . ', ' . $_4ur_first_name; $_4ur_data_strings['Rensselaer ID'] = $_POST['rin']; $_4ur_data_strings['Email'] = $_POST['email']; $_4ur_data_strings['Day phone'] = $_POST['phone']; $_4ur_data_strings['Credit Hours'] = $_POST['creditamount']; $_4ur_data_strings['Transcript Course Title'] = 'RCOS - ' . $_POST['title']; $fdf4ur = forge_fdf("", $_4ur_data_strings, $_4ur_data_names, $_4ur_hidden, $_4ur_readonly); file_put_contents($formfile4ur, $fdf4ur); exec('pdftk ' . escapeshellcmd($infile4ur) . ' fill_form ' . escapeshellcmd($formfile4ur) . ' output ' . escapeshellcmd($outfile4ur) . ' flatten'); $outfilemerge = sys_get_temp_dir() . '/urp-4ur-rcos-' . $clean_name . '-' . $clean_title . uniqid() . '.pdf'; exec('pdftk ' . escapeshellcmd($outfileurp) . ' ' . escapeshellcmd($outfile4ur) . ' cat output ' . escapeshellcmd($outfilemerge)); if (file_exists($outfilemerge)) { header('Content-Description: File Transfer'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename=' . basename($outfilemerge)); header('Content-Transfer-Encoding: binary'); header('Expires: 0'); header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); header('Pragma: public'); header('Content-Length: ' . filesize($outfilemerge)); ob_clean(); flush();
<?php require_once 'forge_fdf.php'; // use this array for text fields, combo box, and list box form field values $fdf_data_strings = array('city' => 'San Francisco', 'state' => 'California'); // use this array for check box and radio button values $fdf_data_names = array(); // these aren't used in this example $fields_hidden = array(); $fields_readonly = array(); $fdf = forge_fdf('', $fdf_data_strings, $fdf_data_names, $fields_hidden, $fields_readonly); $fdf_fn = tempnam('.', 'fdf'); $fp = fopen($fdf_fn, 'w'); if ($fp) { fwrite($fp, $fdf); fclose($fp); // serve PDF, but prompt the user to save it to disk header('Content-type: application/pdf'); header('Content-disposition: attachment; ' . 'filename=filled_form.pdf'); // our pdftk magic; "flatten" merges data with the page passthru('pdftk form.pdf fill_form ' . $fdf_fn . ' output - flatten'); unlink($fdf_fn); // delete temp file } else { // error echo 'Error: unable to write temp fdf file: ' . $fdf_fn; }