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.
This commit is contained in:
Calixte Denizet 2025-07-11 12:55:21 +02:00
parent 2e0f1ec515
commit 1c15ba7789
2 changed files with 20 additions and 11 deletions

View File

@ -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,7 +3184,11 @@ class InternalRenderTask {
}
const { viewport, transform, background } = this.params;
const canvasContext = this._canvas.getContext("2d", {
// 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,
});

View File

@ -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",