You are here

function fillpdf_parse_pdf in FillPDF 6

Same name and namespace in other branches
  1. 7.2 fillpdf.module \fillpdf_parse_pdf()
  2. 7 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_forms_submit in ./fillpdf.admin.inc
Creates a new Form from the uploaded PDF, including parsed fields

File

./fillpdf.module, line 545
Allows mappings of PDFs to site content

Code

function fillpdf_parse_pdf($fid) {
  $filename = db_result(db_query("SELECT url FROM {fillpdf_forms} WHERE fid=%d", $fid));
  $content = _fillpdf_get_file_contents($filename, '<front>');
  $fillpdf_remote_service = variable_get('fillpdf_remote_service', true);
  $fillpdf_local_service = variable_get('fillpdf_local_service', TRUE);

  // use fillpdf-service.com's xmlrpc service (must be registered)
  if ($fillpdf_remote_service) {
    $result = _fillpdf_xmlrpc_request(DEFAULT_SERVLET_URL, 'parse_pdf_fields', base64_encode($content));
    if ($result->error == true) {
      drupal_goto("admin/content/fillpdf");
    }

    //after setting error message
    $fields = $result->data;
  }
  elseif ($fillpdf_local_service) {
    $require = drupal_get_path('module', 'fillpdf') . '/lib/JavaBridge/java/Java.inc';
    require_once $require;
    try {
      $fillpdf = new java('com.ocdevel.FillpdfService', base64_encode($content), 'bytes');
      $fields = java_values($fillpdf
        ->parse());
    } catch (JavaException $e) {
      drupal_set_message(java_truncate((string) $e), 'error');
      drupal_goto("admin/content/fillpdf");

      //after setting error message
    }
  }
  else {
    $fields = fillpdf_execute_parse('pdftk', $filename);
  }

  //create fields
  foreach ((array) $fields as $key => $arr) {
    if ($arr['type']) {

      // Don't store "container" fields
      $arr['name'] = str_replace('&#0;', '', $arr['name']);

      // pdftk sometimes inserts random &#0; markers - strip these out. NOTE: This may break forms that actually DO contain this pattern, but 99%-of-the-time functionality is better than merge failing due to improper parsing.
      $field = new stdClass();
      $field->fid = $fid;
      $field->pdf_key = $arr['name'];
      $field->label = $arr['name'];
      drupal_write_record('fillpdf_fields', $field);
    }
  }
}