You are here

public function PdftkPdfBackend::parseFile in FillPDF 8.4

Same name and namespace in other branches
  1. 5.0.x src/Plugin/PdfBackend/PdftkPdfBackend.php \Drupal\fillpdf\Plugin\PdfBackend\PdftkPdfBackend::parseFile()

Parse a PDF and return a list of its fields.

@todo Replace output array by a value object.

Parameters

\Drupal\file\FileInterface $template_file: The PDF template whose fields are to be parsed.

Return value

string[][] An array of associative arrays. Each sub-array contains a 'name' key with the name of the field and a 'type' key with the type. These can be iterated over and saved by the caller.

Overrides PdfBackendInterface::parseFile

See also

\Drupal\fillpdf\Plugin\PdfBackendInterface::parseStream()

2 calls to PdftkPdfBackend::parseFile()
PdftkPdfBackend::parse in src/Plugin/PdfBackend/PdftkPdfBackend.php
PdftkPdfBackend::parseStream in src/Plugin/PdfBackend/PdftkPdfBackend.php
Parse a PDF and return a list of its fields.

File

src/Plugin/PdfBackend/PdftkPdfBackend.php, line 120

Class

PdftkPdfBackend
Pdftk PdfBackend plugin.

Namespace

Drupal\fillpdf\Plugin\PdfBackend

Code

public function parseFile(FileInterface $template_file) {
  $template_uri = $template_file
    ->getFileUri();
  $pdftk_path = $this
    ->getPdftkPath();
  $status = FillPdf::checkPdftkPath($pdftk_path);
  if ($status === FALSE) {
    $this
      ->messenger()
      ->addError($this
      ->t('pdftk not properly installed.'));
    return [];
  }

  // Escape the template's realpath.
  $template_path = $this->shellManager
    ->escapeShellArg($this->fileSystem
    ->realpath($template_uri));

  // 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_utf8().
  $output = [];
  exec("{$pdftk_path} {$template_path} dump_data_fields_utf8", $output, $status);
  if (count($output) === 0) {
    $this
      ->messenger()
      ->addWarning($this
      ->t('PDF does not contain fillable fields.'));
    return [];
  }

  // Build a simple map of dump_data_fields_utf8 keys to our own array keys.
  $data_fields_map = [
    'FieldType' => 'type',
    'FieldName' => 'name',
    'FieldFlags' => 'flags',
    'FieldValue' => 'value',
    '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.
    list($key, $value) = explode(':', $lineitem);
    if (in_array($key, array_keys($data_fields_map), NULL)) {

      // Trim spaces.
      $fields[$fieldindex][$data_fields_map[$key]] = trim($value);
    }
  }
  return $fields;
}