From d0312aa6ba8b2f412bd386e0b1131c23cedb8e96 Mon Sep 17 00:00:00 2001 From: Brendan Dahl Date: Tue, 26 Nov 2013 13:55:50 -0800 Subject: [PATCH] Wait for one page to render before fetching all pages. --- web/viewer.js | 44 +++++++++++++++++++++++++++++++------------- 1 file changed, 31 insertions(+), 13 deletions(-) diff --git a/web/viewer.js b/web/viewer.js index 7fb01c8da..a6821f3ca 100644 --- a/web/viewer.js +++ b/web/viewer.js @@ -780,9 +780,14 @@ var PDFView = { }, load: function pdfViewLoad(pdfDocument, scale) { + var self = this; + var onePageRendered = new PDFJS.Promise(); function bindOnAfterDraw(pageView, thumbnailView) { // when page is painted, using the image as thumbnail base pageView.onAfterDraw = function pdfViewLoadOnAfterDraw() { + if (!onePageRendered.isResolved) { + onePageRendered.resolve(); + } thumbnailView.setImage(pageView.canvas); }; } @@ -831,7 +836,6 @@ var PDFView = { var thumbnails = this.thumbnails = []; var pagesPromise = this.pagesPromise = new PDFJS.Promise(); - var self = this; var firstPagePromise = pdfDocument.getPage(1); @@ -839,7 +843,6 @@ var PDFView = { // viewport for all pages firstPagePromise.then(function(pdfPage) { var viewport = pdfPage.getViewport((scale || 1.0) * CSS_UNITS); - var pagePromises = []; for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) { var viewportClone = viewport.clone(); var pageView = new PageView(container, pageNum, scale, @@ -850,15 +853,34 @@ var PDFView = { bindOnAfterDraw(pageView, thumbnailView); pages.push(pageView); thumbnails.push(thumbnailView); - if (!PDFJS.disableAutoFetch) { - pagePromises.push(pdfDocument.getPage(pageNum).then( - function (pageView, pdfPage) { - pageView.setPdfPage(pdfPage); - }.bind(this, pageView) - )); - } } + // Fetch all the pages since the viewport is needed before printing + // starts to create the correct size canvas. Wait until one page is + // rendered so we don't tie up too many resources early on. + onePageRendered.then(function () { + if (!PDFJS.disableAutoFetch) { + var getPagesLeft = pagesCount; + for (var pageNum = 1; pageNum <= pagesCount; ++pageNum) { + pdfDocument.getPage(pageNum).then(function (pageNum, pdfPage) { + var pageView = pages[pageNum - 1]; + if (!pageView.pdfPage) { + pageView.setPdfPage(pdfPage); + } + var refStr = pdfPage.ref.num + ' ' + pdfPage.ref.gen + ' R'; + pagesRefMap[refStr] = pageNum; + getPagesLeft--; + if (!getPagesLeft) { + pagesPromise.resolve(); + } + }.bind(null, pageNum)); + } + } else { + // XXX: Printing is semi-broken with auto fetch disabled. + pagesPromise.resolve(); + } + }); + var event = document.createEvent('CustomEvent'); event.initCustomEvent('documentload', true, true, {}); window.dispatchEvent(event); @@ -866,10 +888,6 @@ var PDFView = { PDFView.loadingBar.setWidth(container); PDFFindController.firstPagePromise.resolve(); - - PDFJS.Promise.all(pagePromises).then(function(pages) { - pagesPromise.resolve(pages); - }); }); var prefsPromise = prefs.initializedPromise;