mozilla/pdf.js · error · Error

Invalid rotation

Error message

Invalid rotation

What it means

Thrown by getRotationMatrix for any rotation value that is not exactly 90, 180, or 270 - the switch's default case. Notably, rotation = 0 also throws, because 0 (the identity) is intentionally not handled here; callers are expected to skip the call for 0 and pass the identity matrix themselves.

Source

Thrown at src/core/core_utils.js:736

    date.getUTCDate().toString().padStart(2, "0"),
    date.getUTCHours().toString().padStart(2, "0"),
    date.getUTCMinutes().toString().padStart(2, "0"),
    date.getUTCSeconds().toString().padStart(2, "0"),
  ];

  return buffer.join("");
}

function getRotationMatrix(rotation, width, height) {
  switch (rotation) {
    case 90:
      return [0, 1, -1, 0, width, 0];
    case 180:
      return [-1, 0, 0, -1, width, height];
    case 270:
      return [0, -1, 1, 0, 0, height];
    default:
      throw new Error("Invalid rotation");
  }
}

/**
 * Get the number of bytes to use to represent the given positive integer.
 * If n is zero, the function returns 0 which means that we don't need to waste
 * a byte to represent it.
 * @param {number} x - a positive integer.
 * @returns {number}
 */
function getSizeInBytes(x) {
  // n bits are required for numbers up to 2^n - 1.
  // So for a number x, we need ceil(log2(1 + x)) bits.
  return Math.ceil(Math.ceil(Math.log2(1 + x)) / 8);
}

export {
  arrayBuffersToBytes,

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Normalize before calling: r = ((rotation % 360) + 360) % 360.
  2. Skip the call when r === 0 and use the identity matrix [1, 0, 0, 1, 0, 0].
  3. Reject or clamp non-multiple-of-90 values upstream before they reach this function.

Example fix

// before
const m = getRotationMatrix(rotation, w, h);

// after
const r = ((rotation % 360) + 360) % 360;
const m =
  r === 0
    ? [1, 0, 0, 1, 0, 0]
    : getRotationMatrix(r, w, h);
Defensive patterns

Strategy: validation

Validate before calling

// Normalize rotation and skip the identity case before calling getRotationMatrix
function safeRotationMatrix(rotation, w, h) {
  const r = ((rotation % 360) + 360) % 360;
  if (r === 0) return [1, 0, 0, 1, 0, 0];
  if (r % 90 !== 0) throw new Error(`Unsupported rotation: ${rotation}`);
  return getRotationMatrix(r, w, h);
}

Type guard

function isSupportedRotation(rotation) {
  const r = ((Number(rotation) % 360) + 360) % 360;
  return r === 0 || r === 90 || r === 180 || r === 270;
}

Prevention

When it happens

Trigger: Calling getRotationMatrix(0, w, h) (forgetting that 0 is unsupported), or passing a non-multiple of 90 (45, 360, -90) or an unnormalized raw /Rotate value read straight from a PDF page dictionary.

Common situations: Reading /Rotate from a PDF page dict without normalizing; passing user-supplied rotation through without validation; forgetting the 0-identity special case that every internal caller guards with `rotation !== 0 ? getRotationMatrix(...) : undefined`.

Related errors


AI-assisted analysis of mozilla/pdf.js@5903d58d58 (2026-08-13). Data as JSON: /api/errors/f54c899df9516db6. Report an issue: GitHub.