You are here

function fillpdf_parse_pdf in FillPDF 7

Same name and namespace in other branches
  1. 6 fillpdf.module \fillpdf_parse_pdf()
  2. 7.2 fillpdf.module \fillpdf_parse_pdf()

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 parses the XML response & contsructs the fields.

1 call to fillpdf_parse_pdf()
_fillpdf_save_upload in ./fillpdf.admin.inc

File

./fillpdf.module, line 1629

Code

function fillpdf_parse_pdf($fid) {
  $filename = fillpdf_load($fid);
  $filename = $filename->url;
  $method = variable_get('fillpdf_service');
  if (empty($method)) {
    drupal_set_message(t('FillPDF is not configured.'), 'error');
    drupal_goto('admin/structure/fillpdf');
  }
  $parsed_fields = fillpdf_execute_parse($method, $filename);

  // Redirect back to the administrative page upon error.
  if ($parsed_fields === FALSE) {
    drupal_goto('admin/structure/fillpdf');
  }

  // Delete any existing fields (in case the PDF has been parsed before).
  db_delete('fillpdf_fields')
    ->condition('fid', $fid)
    ->execute();

  // Create fields.
  $unique_fields = array();
  foreach ((array) $parsed_fields as $key => $field) {

    // Don't store "container" fields.
    if (!empty($field['type'])) {

      // Use the field name as key, so to consolidate duplicate fields.
      $unique_fields[$field['name']] = TRUE;
    }
  }

  // Save the fields that were parsed out (if any).
  foreach (array_keys($unique_fields) as $pdf_key) {
    $record = array(
      'label' => NULL,
      'value' => '',
    );
    fillpdf_fields_create_update($fid, $pdf_key, $record);
  }
}