You are here

function fillpdf_generate_fields_from_pdf in FillPDF 5

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 string reference to 'fillpdf_generate_fields_from_pdf'
fillpdf_menu in ./fillpdf.module
Implementation of hook_menu().

File

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

Code

function fillpdf_generate_fields_from_pdf($fid) {

  //currently disabled file upload

  /*$file = file_check_upload('pdf_upload');
  	//handle the file, using file_save_upload, or something similar
  	if (!$file) return;

  	//save the node (we need the nid before uploading)
  	$title->title=$file->filename;
  	
  	$filename="{$title}.pdf";
  	$file = file_save_upload($file,"fillpdf/{$filename}");
  	// get form fields from web service / CGI script
  	*/
  $form_url = db_result(db_query("SELECT url FROM {fillpdf_forms} WHERE fid=%d", $fid));
  $handle = fopen(SERVLET_URL . "?method=parse&form_url={$form_url}", "rb");
  $contents = stream_get_contents($handle);
  fclose($handle);

  // parse XML
  $xml = simplexml_load_string($contents);

  // webform_129.pdf => webform_129 (content-type name)
  $filename = substr($filename, 0, -4);

  //create fields
  $i = 1;
  $weight = -10;
  foreach ($xml->field as $field) {
    $field_type = '';
    switch ((int) $field['type']) {
      case 5:

      //List
      case 6:

        //Combobox
        $field_type = 'select';
        break;
      case 3:

      //Radiobutton
      case 2:

        //Checkbox
        $field_type = 'select';
        break;
      case 0:

      //None
      case 1:

      //Pushbutton

      //return;
      case 4:

      //Text
      case 7:

      //Signature
      default:
        $field_type = 'textfield';
        break;
    }

    //		$new_field->label = $new_field->pdf_key = $field['name'];
    //		$new_field->type=$field_type;
    //		$fields[]=$new_field;
    if ($field['name']) {
      if (!db_query("SELECT 1 FROM {fillpdf_fields} WHERE fid=%d AND pdf_key='%s'", $fid, $field['name'])) {
        db_query("INSERT INTO {fillpdf_fields} (fid, pdf_key, label) VALUES(%d, '%s', '%s')", $fid, $field['name'], $field['name']);
      }
    }
    $i++;
  }
  drupal_goto("admin/content/fillpdf/form/{$fid}/list");
}