title: How to display a PDF stream in a browser using javascript
body: <p>I am accessing an existing WCF web service (which returns a PDF as a byte stream) using jquery's ajax methods.</p>
  
  <p>When the call to the service completes, I end up with a javascript variable containing a PDF (the variable has the binary data in, starting "%PDF-1.4...").</p>
  
  <p>I'd like to display this PDF in a new browser window, but I'm having difficulty achieving this. </p>
  
  <p>My research so far shows that I might be able to achieve what I want using a data: uri, so my code that's called when the ajax call completes is as follows:</p>
  
  <pre><code>function GotPDF(data)
  {
      // Here, data contains "%PDF-1.4 ..." etc.
      var datauri = 'data:application/pdf;base64,' + Base64.encode(data);
      var win = window.open("", "Your PDF", "width=1024,height=768,resizable=yes,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no");
      win.document.location.href = datauri;
  }
  </code></pre>
  
  <p>This causes a new browser window to open, but the contents are blank.</p>
  
  <p>Interestingly, if I point my browser (IE9) at an existing file on my local disk by using a file: uri, such as file://c:/tmp/example.pdf, then I get the same result, i.e. a blank window.</p>
  
  <p>Is there any way I can display this PDF data?</p>