title: How to render whole pdf document using pdf.js library?
body: <p>I tried rendering PDF document using pdf.js library. I know only basics in javascript and I am new to promises, so at first I followed advice on this page: <a href="https://stackoverflow.com/questions/15341010/render-pdf-to-single-canvas-using-pdf-js-and-imagedata">Render .pdf to single Canvas using pdf.js and ImageData</a> (2. answer). <br/> But as a result, I rendered my document with all pages blank. All pictures and colors are fine, but not even a line of text. I also tried some other tutorials, but either I get the same result, or the document is completely missing. 
  Right now, my code looks like this: (It's almost identical to the tutorial)</p>
  
  <pre class="lang-js prettyprint-override"><code>function loadPDFJS(pid, pageUrl){
  
      PDFJS.disableWorker = true;
      PDFJS.workerSrc = 'pdfjs/build/pdf.worker.js';
  
      var canvas = document.createElement('canvas');
      var ctx = canvas.getContext('2d');
      var pages = [];
      var currentPage = 1;
  
      var url = '/search/nimg/IMG_FULL/' + pid + '#page=1';
  
      PDFJS.getDocument(url).then(function (pdf) {
  
          if(currentPage &lt;= pdf.numPages) getPage();
  
          function getPage() {
              pdf.getPage(currentPage).then(function(page){
                  var scale = 1.5;
                  var viewport = page.getViewport(scale);
  
                  canvas.height = viewport.height;
                  canvas.width = viewport.width;
  
                  var renderContext = {
                      canvasContext: ctx,
                      viewport: viewport
                  };
  
                  page.render(renderContext).then(function() {
                      pages.push(canvas.toDataURL());
                      if(currentPage &lt; pdf.numPages) {
                          currentPage++;
                          getPage();
                      } else {
                          done();
                      }
                  });
              });
          }
      });
  
      function done() {
          for(var i = 0; i &lt; pages.length; i++){
              drawPage(i, addPage);
          }
      }
  
      function addPage(img){
          document.body.appendChild(img);
      }
  
      function drawPage(index, callback){
          var img = new Image;
          img.onload = function() {
              ctx.drawImage(this, 0, 0, ctx.canvas.width, ctx.canvas.height);
              callback(this);
          }
          img.src = pages[index];
      }
  }
  </code></pre>