Skip to content
Snippets Groups Projects
Commit 7a76f0b3 authored by Kevin Kaland's avatar Kevin Kaland
Browse files

Issue #1430044: Add pdftk path option.

parent 4d2d5aa5
No related branches found
No related tags found
No related merge requests found
...@@ -36,14 +36,10 @@ function fillpdf_settings($form, &$form_state) { ...@@ -36,14 +36,10 @@ function fillpdf_settings($form, &$form_state) {
} }
// Check for pdftk. // Check for pdftk.
$output = array(); $status = fillpdf_pdftk_check(variable_get('fillpdf_pdftk_path', 'pdftk'));
$status = NULL; if ($status === FALSE) {
exec('pdftk', $output, $status);
if (in_array($status, array(126, 127))) {
$options['pdftk'] .= '<div class="messages warning">' . t('pdftk is not properly installed.') . '</div>'; $options['pdftk'] .= '<div class="messages warning">' . t('pdftk is not properly installed.') . '</div>';
} }
unset($output);
unset($status);
$form['fillpdf_service'] = array( $form['fillpdf_service'] = array(
'#type' => 'radios', '#type' => 'radios',
...@@ -75,12 +71,26 @@ function fillpdf_settings($form, &$form_state) { ...@@ -75,12 +71,26 @@ function fillpdf_settings($form, &$form_state) {
'http' => t('Do not use HTTPS'), 'http' => t('Do not use HTTPS'),
), ),
); );
$form['fillpdf_pdftk_path'] = array(
'#type' => 'textfield',
'#title' => t('Configure path to pdftk'),
'#description' => t("If Fill PDF is not detecting your pdftk installation, you can specify the full path to the program here. Include the program name as well. For example, <em>/usr/bin/pdftk</em> is a valid value. You can almost always leave this field blank. If you should set it, you'll probably know."),
'#default_value' => variable_get('fillpdf_pdftk_path'),
);
drupal_add_js(drupal_get_path('module', 'fillpdf') . '/fillpdf.js'); drupal_add_js(drupal_get_path('module', 'fillpdf') . '/fillpdf.js');
return system_settings_form($form); return system_settings_form($form);
} }
function fillpdf_settings_validate($form, &$form_state) {
$status = fillpdf_pdftk_check($form_state['values']['fillpdf_pdftk_path']);
if ($status === FALSE) {
form_set_error('fillpdf_pdftk_path', t('The path you have entered for
<em>pdftk</em> is invalid. Please enter a valid path.'));
}
}
/* ---------------- Form Create --------------------*/ /* ---------------- Form Create --------------------*/
......
...@@ -713,9 +713,11 @@ function fillpdf_execute_merge($method, $fields, $fillpdf, $mode = 'url', $flatt ...@@ -713,9 +713,11 @@ function fillpdf_execute_merge($method, $fields, $fillpdf, $mode = 'url', $flatt
$xfdf = create_xfdf(basename($xfdfname), $fields); $xfdf = create_xfdf(basename($xfdfname), $fields);
// Generate the file // Generate the file
$xfdffile = file_save_data($xfdf, $xfdfname, FILE_EXISTS_RENAME); $xfdffile = file_save_data($xfdf, $xfdfname, FILE_EXISTS_RENAME);
// Now feed this to pdftk and save the result to a variable // Now feed this to pdftk and save the result to a variable
$path_to_pdftk = variable_get('fillpdf_pdftk_path', 'pdftk');
ob_start(); ob_start();
passthru('pdftk ' . escapeshellarg(drupal_realpath($filename)) . ' fill_form ' . escapeshellarg(drupal_realpath($xfdffile->uri)) . ' output - ' . ($flatten ? 'flatten ' : '') . 'drop_xfa'); passthru($path_to_pdftk . ' ' . escapeshellarg(drupal_realpath($filename)) . ' fill_form ' . escapeshellarg(drupal_realpath($xfdffile->uri)) . ' output - ' . ($flatten ? 'flatten ' : '') . 'drop_xfa');
$data = ob_get_clean(); $data = ob_get_clean();
if ($data === FALSE) { if ($data === FALSE) {
drupal_set_message(t('pdftk not properly installed. No PDF generated.'), 'error'); drupal_set_message(t('pdftk not properly installed. No PDF generated.'), 'error');
...@@ -734,7 +736,6 @@ function fillpdf_execute_merge($method, $fields, $fillpdf, $mode = 'url', $flatt ...@@ -734,7 +736,6 @@ function fillpdf_execute_merge($method, $fields, $fillpdf, $mode = 'url', $flatt
} }
} }
/** /**
* This function generates the form fields from the specified PDF. It (1) sends a request to the iText * This function generates the form fields from the specified PDF. It (1) sends a request to the iText
* servlet to parse the specified PDF, (2) iText returns an XML response with fields-mappings, this module * servlet to parse the specified PDF, (2) iText returns an XML response with fields-mappings, this module
...@@ -810,15 +811,18 @@ function fillpdf_execute_parse($method, $fillpdf, $mode = 'url') { ...@@ -810,15 +811,18 @@ function fillpdf_execute_parse($method, $fillpdf, $mode = 'url') {
$filename = file_unmanaged_save_data($fillpdf, file_directory_temp() . '/pdf_data.pdf', FILE_EXISTS_RENAME); $filename = file_unmanaged_save_data($fillpdf, file_directory_temp() . '/pdf_data.pdf', FILE_EXISTS_RENAME);
break; break;
} }
// Use exec() to call pdftk (because it will be easier to go line-by-line parsing the output) and pass $content via stdin. Retrieve the fields with dump_data_fields.
$output = array(); $path_to_pdftk = variable_get('fillpdf_pdftk_path', 'pdftk');
$status = NULL; $status = fillpdf_pdftk_check($path_to_pdftk);
exec('pdftk ' . escapeshellarg(drupal_realpath($filename)) . ' dump_data_fields', $output, $status); if ($status === FALSE) {
if (in_array($status, array(126, 127))) {
drupal_set_message(t('pdftk not properly installed.'), 'error'); drupal_set_message(t('pdftk not properly installed.'), 'error');
return array(); return array();
} }
elseif (count($output) === 0) {
// Use exec() to call pdftk (because it will be easier to go line-by-line parsing the output) and pass $content via stdin. Retrieve the fields with dump_data_fields.
$output = array();
exec($path_to_pdftk . ' ' . escapeshellarg(drupal_realpath($filename)) . ' dump_data_fields', $output, $status);
if (count($output) === 0) {
drupal_set_message(t('PDF does not contain fillable fields.'), 'warning'); drupal_set_message(t('PDF does not contain fillable fields.'), 'warning');
return array(); return array();
} }
...@@ -1001,3 +1005,13 @@ function fillpdf_load($fid, $reset = FALSE) { ...@@ -1001,3 +1005,13 @@ function fillpdf_load($fid, $reset = FALSE) {
} }
} }
function fillpdf_pdftk_check($pdftk_path = 'pdftk') {
$output = array();
$status = NULL;
exec($pdftk_path, $output, $status);
if (in_array($status, array(126, 127))) {
return FALSE;
}
return TRUE;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment