parallax/jsPDF · error · Error

Invalid format: ${format}

Error message

Invalid format: ${format}

What it means

In html.js's page-format resolver, if the format is not a known named page size (a4, letter, etc.), the code tries to index it as an array (format[1], format[0]). If format is not array-like (a number, null, undefined, or an object without numeric indices), indexing throws and the catch block re-throws 'Invalid format: <format>'. The error is only reached for non-named, non-array formats.

Source

Thrown at src/modules/html.js:966

      case "ex":
        k = 6;
        break;
      default:
        throw "Invalid unit: " + unit;
    }
    var pageHeight = 0;
    var pageWidth = 0;

    // Dimensions are stored as user units and converted to points on output
    if (pageFormats.hasOwnProperty(format_as_string)) {
      pageHeight = pageFormats[format_as_string][1] / k;
      pageWidth = pageFormats[format_as_string][0] / k;
    } else {
      try {
        pageHeight = format[1];
        pageWidth = format[0];
      } catch (err) {
        throw new Error("Invalid format: " + format);
      }
    }

    var tmp;
    // Handle page orientation
    if (orientation === "p" || orientation === "portrait") {
      orientation = "p";
      if (pageWidth > pageHeight) {
        tmp = pageWidth;
        pageWidth = pageHeight;
        pageHeight = tmp;
      }
    } else if (orientation === "l" || orientation === "landscape") {
      orientation = "l";
      if (pageHeight > pageWidth) {
        tmp = pageWidth;
        pageWidth = pageHeight;
        pageHeight = tmp;

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Use a known lowercase named size: 'a4', 'letter', 'legal', etc.
  2. Pass a two-element numeric array [width, height] in the current unit for custom sizes.
  3. Verify the format value type before calling (string name OR [number, number]).
  4. Watch case: the pageFormats keys are lowercase.

Example fix

// before
const doc = new jsPDF({ format: 'A4' }); // 'A4' not in lowercase table -> throws [117]

// after
const doc = new jsPDF({ format: 'a4' });
// or custom:
const doc = new jsPDF({ format: [210, 297], unit: 'mm' });
Defensive patterns

Strategy: validation

Validate before calling

const KNOWN_FORMATS = new Set(['a0','a1','a2','a3','a4','a5','a6','a7','a8','a9','a10','b0','b1','b2','b3','b4','b5','b6','b7','b8','b9','b10','c0','c1','c2','c3','c4','c5','c6','c7','c8','c9','c10','dl','letter','government-letter','legal','junior-legal','ledger','tabloid','credit-card']);
function validFormat(f) {
  if (typeof f === 'string') return KNOWN_FORMATS.has(f.toLowerCase());
  if (Array.isArray(f) && f.length === 2 && f.every(Number.isFinite)) return true;
  return false;
}

Type guard

function isPageFormat(f) {
  return (typeof f === 'string') || (Array.isArray(f) && f.length === 2 && f.every(v => typeof v === 'number' && Number.isFinite(v)));
}

Try / catch

try {
  const doc = new jsPDF({ format });
} catch (e) {
  if (/Invalid format/.test(e.message)) {
    // fall back to a known size or a [w,h] array
    const doc = new jsPDF({ format: 'a4' });
  } else throw e;
}

Prevention

When it happens

Trigger: Calling the html() page setup (or jsPDF constructor routed through html.js) with format set to a number, a single number string, null, undefined, an empty array, or a bare object; passing a dimension array in the wrong order/shape; passing a custom format that is not [w, h].

Common situations: Typo'd page-size names ('A4' vs 'a4' — the table is lowercase); passing pixels as a bare number instead of ['595','842']; migrating from a config that used a different shape.

Related errors


AI-assisted analysis of parallax/jsPDF@a3930ce03a (2026-08-13). Data as JSON: /api/errors/2229d1da8fafb8ca. Report an issue: GitHub.