title: Download in browser with ajax request
body: <p>I'm working with AngularJS and I'm trying to generate a PDF in php. This is what I have in my <em>controller.js</em>:</p>
  
  <pre><code>$scope.downloadPDF = function(){
      $http.post('/download-pdf', { fid: $routeParams.fid })
          .success(function(data, status, headers, config){
              console.log("success");
          })
          .error(function(data, status, headers, config){
              console.log("error");
          });
  };
  </code></pre>
  
  <p>In my php file I have the following to create a PDF with <a href="http://www.fpdf.org/">FPDF library</a>:</p>
  
  <pre><code>function download_pdf()
  {
      $id = $_POST['fid'];
  
      $pdf = new FPDF();
  
      $pdf-&gt;AddPage();
      $pdf-&gt;SetFont('Arial','B',16);
      $pdf-&gt;Cell(40,10,'Hello World!');
  
      header('Content-Type: application/pdf');
      header('Content-Disposition: attachment; filename=' . $id . '.pdf');
  
      $pdf-&gt;Output('Order123.pdf', 'D');
  }
  </code></pre>
  
  <p>But the request is responding this instead of open a save dialog to save my pdf.</p>
  
  <blockquote>
    <p>%PDF-1.3
        3 0 obj
        &lt;>
        endobj
        4 0 obj
        &lt;>
        stream
        x3Rðâ2Ð35W(çr
        QÐw3T04Ó30PISp
            êZ*[¤(hx¤æää+çå¤(j*dÔ7W
        endstream
        endobj
        1 0 obj
        &lt;
        endobj
        5 0 obj
        &lt;
        endobj
        2 0 obj
        &lt;&lt;
        /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
        /Font &lt;&lt;
        /F1 5 0 R
    >
        /XObject &lt;&lt;
    >
    >
        endobj
        6 0 obj
        &lt;&lt;
        /Producer (FPDF 1.7)
        /CreationDate (D:20150611094522)
    >
        endobj
        7 0 obj
        &lt;&lt;
        /Type /Catalog
        /Pages 1 0 R
    >
        endobj
        xref
        0 8
        0000000000 65535 f 
        0000000228 00000 n 
        0000000416 00000 n 
        0000000009 00000 n 
        0000000087 00000 n 
        0000000315 00000 n 
        0000000520 00000 n 
        0000000595 00000 n 
        trailer
        &lt;&lt;
        /Size 8
        /Root 7 0 R
        /Info 6 0 R
    >
        startxref
        644
        %%EOF</p>
  </blockquote>
  
  <p>I've used the <a href="https://phpexcel.codeplex.com/">PHPExcel</a> library and this worked: </p>
  
  <pre><code>$objWriter = PHPExcel_IOFactory::createWriter($ea, 'Excel2007');
  
  // We'll be outputting an excel file
  header('Content-type: application/vnd.ms-excel');
  
  // It will be called Submission on [date_now].xls
  header('Content-Disposition: attachment; filename="' . $filename . '.xls' . '"');
  
  // Write file to the browser
  $objWriter-&gt;save('php://output');
  </code></pre>
  
  <p>Now how can I make this work for my PDF?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p>I've edited my code to this:</p>
  
  <pre><code>$pdf = new FPDF();
  
  $pdf-&gt;AddPage();
  $pdf-&gt;SetFont('Arial','B',16);
  $pdf-&gt;Cell(40,10,'Hello World!');
  
  $filename = DXS_VKGROUP_PLUGIN_LIB_DIR . 'uploads/' . $_POST['fid'] . '.pdf';
  
  $pdf-&gt;Output($filename, 'F'); // Save file locally
  
  header('Pragma: public');
  header('Expires: 0');
  header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  header('Content-Type: application-download');
  header('Content-Length: ' . filesize($filename));
  header('Content-Transfer-Encoding: binary');
  header('Content-Disposition: attachment; filename="' . $filename . '"');
  
  $handle = fopen($filename, 'rb');
  fpassthru($handle);
  fclose($handle);
  </code></pre>
  
  <p>The file is saved locally but the download doesn't work. It doesn't get my a dialog to save the pdf. What am I doing wrong?</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>I've now tried to change <code>application-download</code> in <code>application/pdf</code>. He saves the file locally but I don't get a download dialog box.</p>
  
  <p>The response looks like this (when I check <em>Network</em> in Chrome):</p>
  
  <blockquote>
    <p>%PDF-1.3
        3 0 obj
        &lt;>
        endobj
        4 0 obj
        &lt;>
        stream
        x3Rðâ2Ð35W(çr
        QÐw3T04Ó30PISp
            êZ*[¤(hx¤æää+çå¤(j*dÔ7W
        endstream
        endobj
        1 0 obj
        &lt;
        endobj
        5 0 obj
        &lt;
        endobj
        2 0 obj
        &lt;&lt;
        /ProcSet [/PDF /Text /ImageB /ImageC /ImageI]
        /Font &lt;&lt;
        /F1 5 0 R
    >
        /XObject &lt;&lt;
    >
    >
        endobj
        6 0 obj
        &lt;&lt;
        /Producer (FPDF 1.7)
        /CreationDate (D:20150617090832)
    >
        endobj
        7 0 obj
        &lt;&lt;
        /Type /Catalog
        /Pages 1 0 R
    >
        endobj
        xref
        0 8
        0000000000 65535 f 
        0000000228 00000 n 
        0000000416 00000 n 
        0000000009 00000 n 
        0000000087 00000 n 
        0000000315 00000 n 
        0000000520 00000 n 
        0000000595 00000 n 
        trailer
        &lt;&lt;
        /Size 8
        /Root 7 0 R
        /Info 6 0 R
    >
        startxref
        644
        %%EOF</p>
  </blockquote>