/** * @inheritdoc */ public function parse(FillPdfFormInterface $fillpdf_form) { /** @var FileInterface $file */ $file = File::load($fillpdf_form->file->target_id); $filename = $file->getFileUri(); $path_to_pdftk = $this->getPdftkPath(); $status = FillPdf::checkPdftkPath($path_to_pdftk); if ($status === FALSE) { drupal_set_message($this->t('pdftk not properly installed.'), 'error'); return []; } // 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 = []; exec($path_to_pdftk . ' ' . escapeshellarg($this->fileSystem->realpath($filename)) . ' dump_data_fields', $output, $status); if (count($output) === 0) { drupal_set_message($this->t('PDF does not contain fillable fields.'), 'warning'); return []; } // Build a simple map of dump_data_fields keys to our own array keys $data_fields_map = [ 'FieldType' => 'type', 'FieldName' => 'name', 'FieldFlags' => 'flags', 'FieldJustification' => 'justification', ]; // Build the fields array $fields = []; $fieldindex = -1; foreach ($output as $lineitem) { if ($lineitem == '---') { $fieldindex++; continue; } // Separate the data key from the data value $linedata = explode(':', $lineitem); if (in_array($linedata[0], array_keys($data_fields_map), NULL)) { $fields[$fieldindex][$data_fields_map[$linedata[0]]] = trim($linedata[1]); } } return $fields; }
/** * {@inheritdoc} */ public function validateForm(array &$form, FormStateInterface $form_state) { if ($form_state->getValue('pdftk_path')) { $status = FillPdf::checkPdftkPath($form_state->getValue('pdftk_path')); if ($status === FALSE) { $form_state->setErrorByName('pdftk_path', $this->t('The path you have entered for <em>pdftk</em> is invalid. Please enter a valid path.')); } } }