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. * Page render parameters.
* *
* @typedef {Object} RenderParameters * @typedef {Object} RenderParameters
* @property {CanvasRenderingContext2D} canvasContext - Deprecated 2D context of * @property {CanvasRenderingContext2D} canvasContext - 2D context of a DOM
* a DOM Canvas object for backwards compatibility; it is recommended to use * Canvas object for backwards compatibility; it is recommended to use the
* the `canvas` parameter instead. * `canvas` parameter instead.
* @property {HTMLCanvasElement} canvas - A DOM Canvas object. The default value * If the context must absolutely be used to render the page, the canvas must
* is the canvas associated with the `canvasContext` parameter if no value is * be null.
* provided explicitly. * @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 * @property {PageViewport} viewport - Rendering viewport obtained by calling
* the `PDFPageProxy.getViewport` method. * the `PDFPageProxy.getViewport` method.
* @property {string} [intent] - Rendering intent, can be 'display', 'print', * @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. // Only include the required properties, and *not* the entire object.
params: { params: {
canvas, canvas,
canvasContext,
viewport, viewport,
transform, transform,
background, background,
@ -3148,6 +3151,7 @@ class InternalRenderTask {
this._scheduleNextBound = this._scheduleNext.bind(this); this._scheduleNextBound = this._scheduleNext.bind(this);
this._nextBound = this._next.bind(this); this._nextBound = this._next.bind(this);
this._canvas = params.canvas; this._canvas = params.canvas;
this._canvasContext = params.canvas ? null : params.canvasContext;
this._enableHWA = enableHWA; this._enableHWA = enableHWA;
} }
@ -3180,10 +3184,14 @@ class InternalRenderTask {
} }
const { viewport, transform, background } = this.params; const { viewport, transform, background } = this.params;
const canvasContext = this._canvas.getContext("2d", { // When printing in Firefox, we get a specific context in mozPrintCallback
alpha: false, // which cannot be created from the canvas itself.
willReadFrequently: !this._enableHWA, const canvasContext =
}); this._canvasContext ||
this._canvas.getContext("2d", {
alpha: false,
willReadFrequently: !this._enableHWA,
});
this.gfx = new CanvasGraphics( this.gfx = new CanvasGraphics(
canvasContext, canvasContext,

View File

@ -72,7 +72,8 @@ function composePage(
currentRenderTask = null; currentRenderTask = null;
} }
const renderContext = { const renderContext = {
canvas: ctx.canvas, canvasContext: ctx,
canvas: null,
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0], transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
viewport: pdfPage.getViewport({ scale: 1, rotation: size.rotation }), viewport: pdfPage.getViewport({ scale: 1, rotation: size.rotation }),
intent: "print", intent: "print",