2014-05-20 08:53:40 +09:00
|
|
|
/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set shiftwidth=2 tabstop=2 autoindent cindent expandtab: */
|
|
|
|
|
|
|
|
//
|
|
|
|
// See README for overview
|
|
|
|
//
|
|
|
|
|
|
|
|
'use strict';
|
|
|
|
|
2014-06-20 12:20:55 +09:00
|
|
|
// 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 || '../../test/pdfs/liveprogramming.pdf';
|
|
|
|
var scale = +queryParams.scale || 1.5;
|
|
|
|
|
2014-05-20 08:53:40 +09:00
|
|
|
//
|
|
|
|
// Fetch the PDF document from the URL using promises
|
|
|
|
//
|
2014-06-20 12:20:55 +09:00
|
|
|
PDFJS.getDocument(url).then(function(pdf) {
|
2014-05-20 08:53:40 +09:00
|
|
|
var numPages = pdf.numPages;
|
|
|
|
// Using promise to fetch the page
|
|
|
|
|
|
|
|
// For testing only.
|
|
|
|
var MAX_NUM_PAGES = 50;
|
|
|
|
var ii = Math.min(MAX_NUM_PAGES, numPages);
|
|
|
|
|
|
|
|
var promise = Promise.resolve();
|
|
|
|
for (var i = 1; i <= ii; i++) {
|
2014-06-20 12:20:55 +09:00
|
|
|
var anchor = document.createElement('a');
|
|
|
|
anchor.setAttribute('name', 'page=' + i);
|
|
|
|
anchor.setAttribute('title', 'Page ' + i);
|
|
|
|
document.body.appendChild(anchor);
|
|
|
|
|
|
|
|
// Using promise to fetch and render the next page
|
|
|
|
promise = promise.then(function (pageNum, anchor) {
|
2014-05-20 08:53:40 +09:00
|
|
|
return pdf.getPage(pageNum).then(function (page) {
|
|
|
|
var viewport = page.getViewport(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';
|
2014-06-20 12:20:55 +09:00
|
|
|
anchor.appendChild(container);
|
2014-05-20 08:53:40 +09:00
|
|
|
|
|
|
|
var renderContext = {
|
|
|
|
viewport: viewport,
|
|
|
|
pageNum: pageNum,
|
|
|
|
container: container
|
|
|
|
};
|
2014-06-20 12:20:55 +09:00
|
|
|
// the next page fetch will start only after this page rendering is done
|
|
|
|
return page.getOperatorList().then(function (opList) {
|
2014-06-25 05:24:00 +09:00
|
|
|
var svgGfx = new SVGGraphics(page.commonObjs, page.objs);
|
2014-06-20 12:20:55 +09:00
|
|
|
return svgGfx.loadDependencies(opList).then(function (values) {
|
|
|
|
return svgGfx.beginDrawing(renderContext.viewport,
|
|
|
|
renderContext.pageNum, renderContext.container, opList);
|
2014-05-20 08:53:40 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2014-06-20 12:20:55 +09:00
|
|
|
}.bind(null, i, anchor));
|
2014-05-20 08:53:40 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|