mozilla/pdf.js · error · Error

Invalid rotation

Error message

Invalid rotation

What it means

Thrown by AnnotationEditor.getRect's switch statement in its default branch. getRect computes page-space corners for the editor based on this.rotation, which is expected to be one of 0, 90, 180, or 270. Any other value means the editor's rotation field is uninitialized, corrupted, or set to a non-normalized angle (e.g., 45, 360, negative).

Source

Thrown at src/display/editor/editor.js:1645

          x + shiftY + height + pageX,
          pageHeight - y + shiftX + width + pageY,
        ];
      case 180:
        return [
          x - shiftX - width + pageX,
          pageHeight - y + shiftY + pageY,
          x - shiftX + pageX,
          pageHeight - y + shiftY + height + pageY,
        ];
      case 270:
        return [
          x - shiftY - height + pageX,
          pageHeight - y - shiftX - width + pageY,
          x - shiftY + pageX,
          pageHeight - y - shiftX + pageY,
        ];
      default:
        throw new Error("Invalid rotation");
    }
  }

  getRectInCurrentCoords(rect, pageHeight) {
    const [x1, y1, x2, y2] = rect;

    const width = x2 - x1;
    const height = y2 - y1;

    switch (this.rotation) {
      case 0:
        return [x1, pageHeight - y2, width, height];
      case 90:
        return [x1, pageHeight - y1, height, width];
      case 180:
        return [x2, pageHeight - y1, width, height];
      case 270:
        return [x2, pageHeight - y2, height, width];

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Normalize rotation to the nearest quadrant before instantiating the editor: r => ((r % 360) + 360) % 360 then snap to {0,90,180,270}.
  2. Validate the rotation field when deserializing and reject/repair unexpected values.
  3. Never set editor.rotation to non-orthogonal values.

Example fix

// before
editor.rotation = rawRotation;

// after
function normalize(r) {
  r = ((r % 360) + 360) % 360;
  return [0, 90, 180, 270].reduce((best, v) =>
    Math.abs(v - r) < Math.abs(best - r) ? v : best, 0);
}
editor.rotation = normalize(rawRotation);
Defensive patterns

Strategy: validation

Validate before calling

function normalizeRotation(r) {
  r = ((Number(r) || 0) % 360 + 360) % 360;
  return [0, 90, 180, 270].reduce((best, v) =>
    Math.abs(v - r) < Math.abs(best - r) ? v : best, 0);
}
editor.rotation = normalizeRotation(rawRotation);

Type guard

const isCanonicalRotation = (r): r is 0 | 90 | 180 | 270 =>
  r === 0 || r === 90 || r === 180 || r === 270;

Try / catch

null

Prevention

When it happens

Trigger: An editor instance loaded from serialized data whose rotation field is not one of the four canonical values; setting editor.rotation = 45 directly; deserialization from a tampered or malformed annotation.

Common situations: Loading an annotation export with a non-standard rotation; bugs in upstream code that assign arbitrary angles; round-tripping through tools that do not normalize rotation.

Related errors


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