From ebf493f72680e28ca119601c91264021a85d08b4 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 14 Apr 2023 09:42:03 +0200 Subject: [PATCH] Slightly modernize the print `layout`-methods By getting the width/height of the first page initially, we can slightly reduce the amount of code needed both in the `hasEqualPageSizes`-check and when building the print-styles. --- web/firefox_print_service.js | 16 ++++++---------- web/pdf_print_service.js | 17 ++++++----------- 2 files changed, 12 insertions(+), 21 deletions(-) diff --git a/web/firefox_print_service.js b/web/firefox_print_service.js index ed893c4aa..e78d27f05 100644 --- a/web/firefox_print_service.js +++ b/web/firefox_print_service.js @@ -146,16 +146,13 @@ 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); + const { width, height } = this.pagesOverview[0]; + const hasEqualPageSizes = this.pagesOverview.every( + size => size.width === width && size.height === height + ); if (!hasEqualPageSizes) { console.warn( - "Not all pages have the same size. The printed " + - "result may be incorrect!" + "Not all pages have the same size. The printed result may be incorrect!" ); } @@ -163,8 +160,7 @@ FirefoxPrintService.prototype = { // 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;}`; + this.pageStyleSheet.textContent = `@page { size: ${width}pt ${height}pt;}`; body.append(this.pageStyleSheet); if (pdfDocument.isPureXfa) { diff --git a/web/pdf_print_service.js b/web/pdf_print_service.js index e0401f388..e14f1feb2 100644 --- a/web/pdf_print_service.js +++ b/web/pdf_print_service.js @@ -92,16 +92,13 @@ PDFPrintService.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); + const { width, height } = this.pagesOverview[0]; + const hasEqualPageSizes = this.pagesOverview.every( + size => size.width === width && size.height === height + ); if (!hasEqualPageSizes) { console.warn( - "Not all pages have the same size. The printed " + - "result may be incorrect!" + "Not all pages have the same size. The printed result may be incorrect!" ); } @@ -115,9 +112,7 @@ PDFPrintService.prototype = { // 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 = - "@page { size: " + pageSize.width + "pt " + pageSize.height + "pt;}"; + this.pageStyleSheet.textContent = `@page { size: ${width}pt ${height}pt;}`; body.append(this.pageStyleSheet); },