mozilla/pdf.js · error · Error

PageViewport: Invalid rotation, must be a multiple of 90 deg

Error message

PageViewport: Invalid rotation, must be a multiple of 90 degrees.

What it means

Thrown by the PageViewport constructor when, after normalizing rotation into [0,360), the value is not 0, 90, 180, or 270. PageViewport builds a coordinate transform with an explicit case per valid quadrant, so any non-orthogonal angle cannot be represented and is rejected. This is a caller-input error, not a PDF-content error.

Source

Thrown at src/display/page_viewport.js:108

        rotateA = 0;
        rotateB = 1;
        rotateC = 1;
        rotateD = 0;
        break;
      case 270:
        rotateA = 0;
        rotateB = -1;
        rotateC = -1;
        rotateD = 0;
        break;
      case 0:
        rotateA = 1;
        rotateB = 0;
        rotateC = 0;
        rotateD = -1;
        break;
      default:
        throw new Error(
          "PageViewport: Invalid rotation, must be a multiple of 90 degrees."
        );
    }

    if (dontFlip) {
      rotateC = -rotateC;
      rotateD = -rotateD;
    }

    let offsetCanvasX, offsetCanvasY;
    let width, height;
    if (rotateA === 0) {
      offsetCanvasX = Math.abs(centerY - viewBox[1]) * scale + offsetX;
      offsetCanvasY = Math.abs(centerX - viewBox[0]) * scale + offsetY;
      width = (viewBox[3] - viewBox[1]) * scale;
      height = (viewBox[2] - viewBox[0]) * scale;
    } else {
      offsetCanvasX = Math.abs(centerX - viewBox[0]) * scale + offsetX;

View on GitHub (pinned to 5903d58d58)

Solutions

  1. Pass only one of 0, 90, 180, 270 for rotation.
  2. Use page.rotate (already a valid multiple of 90) rather than computing your own angle.
  3. Clamp/snap a user angle to the nearest multiple of 90 before constructing the viewport.
  4. Validate rotation with a modulo-90 check before constructing the viewport.

Example fix

// before
const viewport = new PageViewport({ viewBox, scale, rotation: 45 });
// after
const viewport = new PageViewport({ viewBox, scale, rotation: 90 });
Defensive patterns

Strategy: validation

Validate before calling

function normalizeRotation(r) {
  const mod = ((r % 360) + 360) % 360;
  return mod % 90 === 0 ? mod : null;
}
const rot = normalizeRotation(rotation);
if (rot === null) throw new Error('rotation must be a multiple of 90');

Type guard

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

Prevention

When it happens

Trigger: new PageViewport({ viewBox, scale, rotation }) is called with rotation not a multiple of 90 (e.g. 45, 12.5, radians).

Common situations: Passing radians instead of degrees; passing a fractional rotation; computing rotation from a free-form angle control; misusing a value pulled from page.rotate after mutating it.

Related errors


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