title: Transfering pdf from backend to frontend on request jQuery
body: <p>as mentioned in the title I am trying to pass pdf from backend php to frontend using <strong>jquery</strong>.</p>
  <p>Sample php code.</p>
  <pre><code>public function printPdf(){
      header('Content-Disposition: attachment; filename=&quot;kainos.pdf&quot;');
      $html = $this-&gt;request-&gt;post['pdf'];
      $mpdf = new \Mpdf\Mpdf();
      $mpdf-&gt;WriteHTML('&lt;div&gt;Section 1 text&lt;/div&gt;');
      return $mpdf-&gt;Output();
  }
  </code></pre>
  <pre><code>function(response){
      var blob=new Blob([response], { type: 'application/pdf' });
      var link=document.createElement('a');
      link.href=window.URL.createObjectURL(blob);
      link.download=&quot;Kainos.pdf&quot;;
      link.click();
  }
  </code></pre>
  <p>What i get is empty pdf.</p>
  <p>What i know :</p>
  <p>Data is being transfered correctly, since plain html can be transfered and seen using javascript console.log</p>
  <p>Pdf is being generated, since i can just move the contents of the function to other function and get the pdf in other page, but i don't need it there.</p>
  <p>I suspect that pdf is getting destroyed while being transfered, but can't seem to find any information
  on how to fix it. Any help is appreciated</p>
  EDIT
  <p>Problem was not in the method I am sending but in jquery, it handles blobs wierd, so I am in the middle of rewriting everything in vanila js.</p>
  <pre><code>var xhr = new XMLHttpRequest();
  xhr.open('POST', document.location.origin + '/somelocation.php', true);
      //Send data to server
      xhr.send({$(&quot;#pdf&quot;).html()});
      //Set response type to file
      xhr.responseType = &quot;blob&quot;;
      
      //On response do...
      xhr.onload = () =&gt; {
          //Create file in client side from xhr response
          var blob=new Blob([xhr.response], { type: 'application/pdf' });
          //create clickable element to download file and click it.
          var link=document.createElement('a');
          link.href=window.URL.createObjectURL(blob);
          link.download=&quot;kainos.pdf&quot;;
          link.click();
      }
  });
  </code></pre>