title: Downloading PDF file with JQuery AJAX and PHP
body: <p>I already found <a href="https://stackoverflow.com/questions/3613526/php-file-download-using-post-data-via-jquery-ajax">many</a> <a href="https://stackoverflow.com/questions/3599670/ajax-file-download-using-jquery-php">questions</a> <a href="https://stackoverflow.com/questions/13701297/forcing-a-download-using-php-jquery">here</a> in SO about the theme, but any of them helps me with a specific problem.</p>
  
  <p>I need to send a ajax request to PHP and then download a PDF file.</p>
  
  <p>This is my Javascript</p>
  
  <pre><code>$('body').on("click",'.download-file', function(e) {
          e.stopPropagation();
          var id = $(this).attr('data-id');
              $.ajax({
                  url: 'app/myPage/download',// The framework will redirect it to index.php and then to myPage.class and method dowload
                  data: id,
                  type: 'POST',
                  success: function (return) {
                     console.log(return);
                  },
                  error: function(jqXHR, textStatus, errorThrown) {
                     console.log("Error... " + textStatus + "        " + errorThrown);
                  }
              });
      });
  </code></pre>
  
  <p>Here is my PHP file:</p>
  
  <pre><code>class myPage{
      public function download()
         {
              $id = $_POST['id'];
              $filePath = $this-&gt;methodToGetFile($id);
              $name = end(explode('/',$filePath));
              $fp = fopen($filePath, 'rb');
              header("Cache-Control: ");
              header("Pragma: ");
              header("Content-Type: application/octet-stream");
              header("Content-Length: " . filesize($filePath));
              header("Content-Disposition: attachment; filename='".$name."';");
              header("Content-Transfer-Encoding: binary\n");
              fpassthru($fp);
              exit;
      }
  }
  </code></pre>
  
  <p>With this approach, I have the PDF "printed" in the console like </p>
  
  <p><code>%PDF-1.5%����1 0 obj&lt;&lt;/Type/Catalog/Pages 2 0 R/Lang(pt-BR) /StructTreeRoot 8 0 R/MarkInfo&lt;&lt;/Marked true&gt;&gt;&gt;&gt; ...</code>.</p>
  
  <p>So, I'd like to know what should I do to open this file and download it. </p>
  
  <p>In the linked questions I see something about <code>window.location</code>, but I don't know how can I use it.</p>
  
  <p>I've already tried change the PHP headers to try force the download, but no success with it. In these cases I just receive null in the javascript nothing happens. Here are the modified headers:</p>
  
  <pre><code>    header('Content-Type: application/pdf');//tried with this option
      //header('Content-Type: application/octet-stream');// also tried with this other option
      header('Content-Disposition: attachment; filename=' . $name);
      header('Content-Transfer-Encoding: binary');
      header('Connection: Keep-Alive');
      header('Expires: 0');
      header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
      header('Pragma: public');
      header('Content-Length: ' . filesize($filePath));
  </code></pre>
  
  <p>Are there some good approach to do this?</p>