title: sending an uploaded PDF file via POST is becoming corrupt
body: <p>Im trying to send a pdf file uploaded using JQ to a PHP file which uses CLI and pdf2svg to convert it to an svg file, with the eventual aim to return that svg file so that it can be placed into svg editor.</p>
  
  <p>Currently i have this:
  In svg-editor.js:</p>
  
  <pre><code>if(file.type.indexOf("pdf") != -1){
      //convert to svg
      //load svg string
      var reader = new FileReader();
      reader.onloadend = function(e) {
          $.post("pdfuploads/pdfconvert.php", { 'pdf[]': [e.target.result] })
              .done(function(data){
                  alert("Data Loaded: " + data );
                  svgCanvas.importSvgString(data, true);
                  svgCanvas.ungroupSelectedElement()
                  svgCanvas.ungroupSelectedElement()
                  svgCanvas.groupSelectedElements()
                  svgCanvas.alignSelectedElements("m", "page")
                  svgCanvas.alignSelectedElements("c", "page")
              });
      };
  reader.readAsText(file);
  }
  </code></pre>
  
  <p>The above checks the extension of the file being loaded, if its pdf then it posts it to the php file. (all working fine)</p>
  
  <p>In my php file (its being tied in with drupal):</p>
  
  <pre><code>&lt;?php
  
  $dir = getcwd();
  define('DRUPAL_ROOT', $_SERVER['DOCUMENT_ROOT']); //added to make sure its defined as we're outside the use of index.php
  chdir(DRUPAL_ROOT);
  require_once './includes/bootstrap.inc';
  drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
  chdir($dir);
  global $base_url;
  
  $time = microtime();
  
  $handle = fopen("pdfupload-".$time.".pdf", "wb"); 
  if (fwrite($handle, file_get_contents($_POST['pdf'][0])) === FALSE) { 
  $error = "Cannot write to file";
  exit; 
  } 
  fclose($handle);
  //$file = file_put_contents('pdfupload-'.$time.'.pdf', $_POST['pdf'][0]);
  $svg = exec("/usr/local/bin/pdf2svg pdfupload-".$time.".pdf output_page-".$time."%d.svg all");
  
  cg_utils_watch('file pdf value','', $error);
  cg_utils_watch('file svg value','', $svg);
  
  echo "&lt;pre&gt;";
  print_r($svg);
  echo "&lt;/pre&gt;";
  
  return $svg;
  </code></pre>
  
  <p>Ignore the logging and the fact that $svg doesnt return the svg file --</p>
  
  <p>The pdf stream is taken from post, and saved. Unfortunalty someware between upload and saving on the server the pdf file becomes corrupted. It saves a pdf file, but when opening that file its empty, the end result being the svg file thats written to the server in the exec() is also empty.</p>
  
  <p>Does anyone have any ideas / know any better way of doing this?</p>