All files / pdf.js/examples/svgviewer viewer.js

0% Statements 0/31
0% Branches 0/4
0% Functions 0/8
0% Lines 0/28
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                                                                                                                     
'use strict';
 
var DEFAULT_SCALE = 1.5;
 
// Parse query string to extract some parameters (it can fail for some input)
var query = document.location.href.replace(/^[^?]*(\?([^#]*))?(#.*)?/, '$2');
var queryParams = query ? JSON.parse('{' + query.split('&').map(function (a) {
  return a.split('=').map(decodeURIComponent).map(JSON.stringify).join(': ');
}).join(',') + '}') : {};
 
var url = queryParams.file || '../../web/compressed.tracemonkey-pldi-09.pdf';
 
function renderDocument(pdf, svgLib) {
  var promise = Promise.resolve();
  for (var i = 1; i <= pdf.numPages; i++) {
    // Using promise to fetch and render the next page
    promise = promise.then(function (pageNum) {
      return pdf.getPage(pageNum).then(function (page) {
        var viewport = page.getViewport(DEFAULT_SCALE);
 
        var container = document.createElement('div');
        container.id = 'pageContainer' + pageNum;
        container.className = 'pageContainer';
        container.style.width = viewport.width + 'px';
        container.style.height = viewport.height + 'px';
        document.body.appendChild(container);
 
        return page.getOperatorList().then(function (opList) {
          var svgGfx = new svgLib.SVGGraphics(page.commonObjs, page.objs);
          return svgGfx.getSVG(opList, viewport).then(function (svg) {
            container.appendChild(svg);
          });
        });
      });
    }.bind(null, i));
  }
}
 
Promise.all([System.import('pdfjs/display/api'),
             System.import('pdfjs/display/svg'),
             System.import('pdfjs/display/global'),
             System.import('pdfjs/display/network'),
             System.resolve('pdfjs/worker_loader')])
       .then(function (modules) {
  var api = modules[0], svg = modules[1], global = modules[2], network = modules[3];
  api.setPDFNetworkStreamClass(network.PDFNetworkStream);
  // In production, change this to point to the built `pdf.worker.js` file.
  global.PDFJS.workerSrc = modules[4];
 
  // In production, change this to point to where the cMaps are placed.
  global.PDFJS.cMapUrl = '../../external/bcmaps/';
  global.PDFJS.cMapPacked = true;
 
  // Fetch the PDF document from the URL using promises.
  api.getDocument(url).then(function (doc) {
    renderDocument(doc, svg);
  });
});