From ff7f87fc213534b609b9230f5c96ae0443072a83 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 30 Jan 2026 15:51:22 +0100 Subject: [PATCH] Replace the `IPDFPrintServiceFactory` interface with an abstract `BasePrintServiceFactory` class This should help reduce the maintenance burden of the code, since you no longer need to remember to update separate code when touching the different `PDFPrintServiceFactory` classes. --- web/app.js | 5 +---- web/firefox_print_service.js | 10 +++++----- web/interfaces.js | 17 +---------------- web/pdf_print_service.js | 13 +++++-------- web/print_utils.js | 20 +++++++++++++++++++- 5 files changed, 31 insertions(+), 34 deletions(-) diff --git a/web/app.js b/web/app.js index 0cd978f3e..056f3fdcd 100644 --- a/web/app.js +++ b/web/app.js @@ -2456,10 +2456,7 @@ const PDFViewerApplication = { }; initCom(PDFViewerApplication); - -if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("MOZCENTRAL")) { - PDFPrintServiceFactory.initGlobals(PDFViewerApplication); -} +PDFPrintServiceFactory.initGlobals(PDFViewerApplication); if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { const HOSTED_VIEWER_ORIGINS = new Set([ diff --git a/web/firefox_print_service.js b/web/firefox_print_service.js index 2af9d8529..04b8921e4 100644 --- a/web/firefox_print_service.js +++ b/web/firefox_print_service.js @@ -19,7 +19,10 @@ import { RenderingCancelledException, shadow, } from "pdfjs-lib"; -import { getXfaHtmlForPrinting } from "./print_utils.js"; +import { + BasePrintServiceFactory, + getXfaHtmlForPrinting, +} from "./print_utils.js"; // Creates a placeholder with div and canvas with right size for the page. function composePage( @@ -194,10 +197,7 @@ class FirefoxPrintService { } } -/** - * @implements {IPDFPrintServiceFactory} - */ -class PDFPrintServiceFactory { +class PDFPrintServiceFactory extends BasePrintServiceFactory { static get supportsPrinting() { const canvas = document.createElement("canvas"); return shadow(this, "supportsPrinting", "mozPrintCallback" in canvas); diff --git a/web/interfaces.js b/web/interfaces.js index 83e0f6c96..c798eba3a 100644 --- a/web/interfaces.js +++ b/web/interfaces.js @@ -71,19 +71,4 @@ class IDownloadManager { download(data, url, filename) {} } -/** - * @interface - */ -class IPDFPrintServiceFactory { - static initGlobals() {} - - static get supportsPrinting() { - return false; - } - - static createPrintService() { - throw new Error("Not implemented: createPrintService"); - } -} - -export { IDownloadManager, IPDFPrintServiceFactory, IRenderableView }; +export { IDownloadManager, IRenderableView }; diff --git a/web/pdf_print_service.js b/web/pdf_print_service.js index c20f928a9..e6ecbd363 100644 --- a/web/pdf_print_service.js +++ b/web/pdf_print_service.js @@ -13,16 +13,16 @@ * limitations under the License. */ -// eslint-disable-next-line max-len -/** @typedef {import("./interfaces.js").IPDFPrintServiceFactory} IPDFPrintServiceFactory */ - import { AnnotationMode, PixelsPerInch, RenderingCancelledException, shadow, } from "pdfjs-lib"; -import { getXfaHtmlForPrinting } from "./print_utils.js"; +import { + BasePrintServiceFactory, + getXfaHtmlForPrinting, +} from "./print_utils.js"; let activeService = null; let dialog = null; @@ -371,10 +371,7 @@ function ensureOverlay() { return overlayPromise; } -/** - * @implements {IPDFPrintServiceFactory} - */ -class PDFPrintServiceFactory { +class PDFPrintServiceFactory extends BasePrintServiceFactory { static initGlobals(app) { viewerApp = app; } diff --git a/web/print_utils.js b/web/print_utils.js index c97af4756..04c820de9 100644 --- a/web/print_utils.js +++ b/web/print_utils.js @@ -17,6 +17,24 @@ import { getXfaPageViewport, PixelsPerInch } from "pdfjs-lib"; import { SimpleLinkService } from "./pdf_link_service.js"; import { XfaLayerBuilder } from "./xfa_layer_builder.js"; +class BasePrintServiceFactory { + constructor() { + if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) { + throw new Error("Cannot initialize BasePrintServiceFactory."); + } + } + + static initGlobals(app) {} + + static get supportsPrinting() { + throw new Error("Not implemented: supportsPrinting"); + } + + static createPrintService(params) { + throw new Error("Not implemented: createPrintService"); + } +} + function getXfaHtmlForPrinting(printContainer, pdfDocument) { const xfaHtml = pdfDocument.allXfaHtml; const linkService = new SimpleLinkService(); @@ -40,4 +58,4 @@ function getXfaHtmlForPrinting(printContainer, pdfDocument) { } } -export { getXfaHtmlForPrinting }; +export { BasePrintServiceFactory, getXfaHtmlForPrinting };