From c5b0b5c754b1b925b1e3b6cff52823e1364c1f5d Mon Sep 17 00:00:00 2001 From: Jonas Jenwald <jonas.jenwald@gmail.com> Date: Tue, 24 Mar 2020 12:58:34 +0100 Subject: [PATCH] Ensure that automatic printing still works when the viewer and/or its pages are hidden (bug 1618621, bug 1618955) Please note that this patch, on its own, won't magically fix all of these printing bugs without [bug 1618553](https://bugzilla.mozilla.org/show_bug.cgi?id=1618553) also being fixed. (However I don't foresee that being too difficult, famous last words :-), but it will as suggested require a platform API that we can notify when the viewer is ready.) Fixes https://bugzilla.mozilla.org/show_bug.cgi?id=1618621 Fixes https://bugzilla.mozilla.org/show_bug.cgi?id=1618955 Fixes 8208 --- web/base_viewer.js | 23 ++++++++++++++++++++++- web/ui_utils.js | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/web/base_viewer.js b/web/base_viewer.js index 4b2fbe734..3b07953b2 100644 --- a/web/base_viewer.js +++ b/web/base_viewer.js @@ -396,6 +396,27 @@ class BaseViewer { throw new Error("Not implemented: _setDocumentViewerElement"); } + /** + * @private + */ + _onePageRenderedOrForceFetch() { + // Unless the viewer *and* its pages are visible, rendering won't start and + // `this._onePageRenderedCapability` thus won't be resolved. + // To ensure that automatic printing, on document load, still works even in + // those cases we force-allow fetching of all pages when: + // - The viewer is hidden in the DOM, e.g. in a `display: none` <iframe> + // element; fixes bug 1618621. + // - The viewer is visible, but none of the pages are (e.g. if the + // viewer is very small); fixes bug 1618955. + if ( + !this.container.offsetParent || + this._getVisiblePages().views.length === 0 + ) { + return Promise.resolve(); + } + return this._onePageRenderedCapability.promise; + } + /** * @param pdfDocument {PDFDocument} */ @@ -492,7 +513,7 @@ class BaseViewer { // 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. - this._onePageRenderedCapability.promise.then(() => { + this._onePageRenderedOrForceFetch().then(() => { if (this.findController) { this.findController.setDocument(pdfDocument); // Enable searching. } diff --git a/web/ui_utils.js b/web/ui_utils.js index e456beda3..1ef9c45ef 100644 --- a/web/ui_utils.js +++ b/web/ui_utils.js @@ -229,7 +229,7 @@ function binarySearchFirstItem(items, condition) { let minIndex = 0; let maxIndex = items.length - 1; - if (items.length === 0 || !condition(items[maxIndex])) { + if (maxIndex < 0 || !condition(items[maxIndex])) { return items.length; } if (condition(items[minIndex])) {