From 1c15ba778984e28e3de91b278a67d1a8dc614261 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Fri, 11 Jul 2025 12:55:21 +0200 Subject: [PATCH] Use the canvas context from mozPrintCallback when printing a pdf from the Firefox viewer The context is specific to the callback and cannot be created from the canvas itself. --- src/display/api.js | 28 ++++++++++++++++++---------- web/firefox_print_service.js | 3 ++- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index e1f39d5ff..f77d97520 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1182,12 +1182,14 @@ class PDFDocumentProxy { * Page render parameters. * * @typedef {Object} RenderParameters - * @property {CanvasRenderingContext2D} canvasContext - Deprecated 2D context of - * a DOM Canvas object for backwards compatibility; it is recommended to use - * the `canvas` parameter instead. - * @property {HTMLCanvasElement} canvas - A DOM Canvas object. The default value - * is the canvas associated with the `canvasContext` parameter if no value is - * provided explicitly. + * @property {CanvasRenderingContext2D} canvasContext - 2D context of a DOM + * Canvas object for backwards compatibility; it is recommended to use the + * `canvas` parameter instead. + * If the context must absolutely be used to render the page, the canvas must + * be null. + * @property {HTMLCanvasElement|null} canvas - A DOM Canvas object. The default + * value is the canvas associated with the `canvasContext` parameter if no + * value is provided explicitly. * @property {PageViewport} viewport - Rendering viewport obtained by calling * the `PDFPageProxy.getViewport` method. * @property {string} [intent] - Rendering intent, can be 'display', 'print', @@ -1503,6 +1505,7 @@ class PDFPageProxy { // Only include the required properties, and *not* the entire object. params: { canvas, + canvasContext, viewport, transform, background, @@ -3148,6 +3151,7 @@ class InternalRenderTask { this._scheduleNextBound = this._scheduleNext.bind(this); this._nextBound = this._next.bind(this); this._canvas = params.canvas; + this._canvasContext = params.canvas ? null : params.canvasContext; this._enableHWA = enableHWA; } @@ -3180,10 +3184,14 @@ class InternalRenderTask { } const { viewport, transform, background } = this.params; - const canvasContext = this._canvas.getContext("2d", { - alpha: false, - willReadFrequently: !this._enableHWA, - }); + // When printing in Firefox, we get a specific context in mozPrintCallback + // which cannot be created from the canvas itself. + const canvasContext = + this._canvasContext || + this._canvas.getContext("2d", { + alpha: false, + willReadFrequently: !this._enableHWA, + }); this.gfx = new CanvasGraphics( canvasContext, diff --git a/web/firefox_print_service.js b/web/firefox_print_service.js index 6f207d180..2af9d8529 100644 --- a/web/firefox_print_service.js +++ b/web/firefox_print_service.js @@ -72,7 +72,8 @@ function composePage( currentRenderTask = null; } const renderContext = { - canvas: ctx.canvas, + canvasContext: ctx, + canvas: null, transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0], viewport: pdfPage.getViewport({ scale: 1, rotation: size.rotation }), intent: "print",