diff --git a/web/firefox_print_service.js b/web/firefox_print_service.js index 37576f660..ed893c4aa 100644 --- a/web/firefox_print_service.js +++ b/web/firefox_print_service.js @@ -146,6 +146,27 @@ FirefoxPrintService.prototype = { const body = document.querySelector("body"); body.setAttribute("data-pdfjsprinting", true); + const hasEqualPageSizes = this.pagesOverview.every(function (size) { + return ( + size.width === this.pagesOverview[0].width && + size.height === this.pagesOverview[0].height + ); + }, this); + if (!hasEqualPageSizes) { + console.warn( + "Not all pages have the same size. The printed " + + "result may be incorrect!" + ); + } + + // Insert a @page + size rule to make sure that the page size is correctly + // set. Note that we assume that all pages have the same size, because + // variable-size pages are scaled down to the initial page size in Firefox. + this.pageStyleSheet = document.createElement("style"); + const pageSize = this.pagesOverview[0]; + this.pageStyleSheet.textContent = `@page { size: ${pageSize.width}pt ${pageSize.height}pt;}`; + body.append(this.pageStyleSheet); + if (pdfDocument.isPureXfa) { getXfaHtmlForPrinting(printContainer, pdfDocument); return; @@ -169,6 +190,11 @@ FirefoxPrintService.prototype = { const body = document.querySelector("body"); body.removeAttribute("data-pdfjsprinting"); + + if (this.pageStyleSheet) { + this.pageStyleSheet.remove(); + this.pageStyleSheet = null; + } }, }; diff --git a/web/pdf_print_service.js b/web/pdf_print_service.js index 87cc73c62..e0401f388 100644 --- a/web/pdf_print_service.js +++ b/web/pdf_print_service.js @@ -111,9 +111,9 @@ PDFPrintService.prototype = { // TODO(robwu): Use named pages when size calculation bugs get resolved // (e.g. https://crbug.com/355116) AND when support for named pages is // added (http://www.w3.org/TR/css3-page/#using-named-pages). - // In browsers where @page + size is not supported (such as Firefox, - // https://bugzil.la/851441), the next stylesheet will be ignored and the - // user has to select the correct paper size in the UI if wanted. + // In browsers where @page + size is not supported, the next stylesheet + // will be ignored and the user has to select the correct paper size in + // the UI if wanted. this.pageStyleSheet = document.createElement("style"); const pageSize = this.pagesOverview[0]; this.pageStyleSheet.textContent = diff --git a/web/pdf_viewer.js b/web/pdf_viewer.js index 6649a3143..533848517 100644 --- a/web/pdf_viewer.js +++ b/web/pdf_viewer.js @@ -1673,21 +1673,27 @@ class PDFViewer { * @returns {Array} Array of objects with width/height/rotation fields. */ getPagesOverview() { + let initialOrientation; return this._pages.map(pageView => { const viewport = pageView.pdfPage.getViewport({ scale: 1 }); - - if (!this.enablePrintAutoRotate || isPortraitOrientation(viewport)) { + const orientation = isPortraitOrientation(viewport); + if (initialOrientation === undefined) { + initialOrientation = orientation; + } else if ( + this.enablePrintAutoRotate && + orientation !== initialOrientation + ) { + // Rotate to fit the initial orientation. return { - width: viewport.width, - height: viewport.height, - rotation: viewport.rotation, + width: viewport.height, + height: viewport.width, + rotation: (viewport.rotation - 90) % 360, }; } - // Landscape orientation. return { - width: viewport.height, - height: viewport.width, - rotation: (viewport.rotation - 90) % 360, + width: viewport.width, + height: viewport.height, + rotation: viewport.rotation, }; }); }