title: PDF.JS: Render PDF using an ArrayBuffer or Blob instead of URL
body: <p>I know of a similar question to this one: <a href="https://stackoverflow.com/questions/12092633/pdf-js-rendering-a-pdf-file-using-a-base64-file-source-instead-of-url">Pdf.js: rendering a pdf file using a base64 file source instead of url</a>. That question was awesomely answered by <a href="https://stackoverflow.com/users/1049459/codetoffel">Codetoffel</a> but my question is different in that my PDF is retrieved via a RESTful call to my Web API implementation. Let me explain...</p>
  
  <p>First, here's a basic way to use PDF.JS to open a PDF via a URL:</p>
  
  <pre><code>PDFJS.getDocument("/api/path/to/my.pdf").then(function (pdf) {
    pdf.getPage(1).then(function (page) {
      var scale = 1;
      var viewport = page.getViewport(scale);
      var canvas = document.getElementById('the-canvas');
      var context = canvas.getContext('2d');
      canvas.height = viewport.height;
      canvas.width = viewport.width;
      page.render({canvasContext: context, viewport: viewport});
    });
  });
  </code></pre>
  
  <p>This works great, but I am using Angular and its <code>$resource</code> service to make the request for the PDF via my RESTful Web API. I do know that PDF.JS allows me to replace passing the URL as a string in the PDFJS.getDocument method (above) with a <code>DocumentInitParams</code> object, which is defined <a href="http://mozilla.github.io/pdf.js/api/draft/global.html" rel="noreferrer">here</a>. Using the DocumentInitParams object works as follows:</p>
  
  <pre><code>var docInitParams = {
      url: "/api/path/to/my.pdf",
      httpHeaders: getSecurityHeaders(), //as needed
      withCredentials: true
  };
  PDFJS.getDocument(docInitParams).then(function (pdf) {
      ...
  });
  </code></pre>
  
  <p>This also works, but it works around my Angular <code>$resource</code> by requiring me to construct the api URL. But that's OK because PDFJS allows me to give it the PDF data directly, instead of giving it a URL to the PDF, as follows:</p>
  
  <pre><code>var myPdf = myService.$getPdf({ Id: 123 });
  
  //It's an Angular $resource, so there is a promise to be fulfilled...
  myPdf.$promise.then(function() {
      var docInitParams = {
          data: myPdf
      };
      PDFJS.getDocument(docInitParams).then(function (pdf) {
          ...
      });
  });
  </code></pre>
  
  <p>This is the one I can't seem to get to work. I can tell the <code>myService.$gtPdf</code> method to return the data as a <code>blob</code> or as an <code>arraybuffer</code> but neither works. I've tried to convert the arraybuffer returned data to an <code>Uint8Array</code> too, but to no avail.</p>
  
  <p>I'm not sure what else to try and could really use a tip.</p>
  
  <p><strong>How do I get the data returned from my service to work with PDFJS?</strong></p>
  
  <p>Thanks in advance.</p>