parallax/jsPDF · error · Error

Invalid Combination of fontweight and fontstyle

Error message

Invalid Combination of fontweight and fontstyle

What it means

`combineFontStyleAndFontWeight` (src/jspdf.js:368) merges a CSS-like fontStyle and fontWeight into the single style string jsPDF stores per font. It throws at src/jspdf.js:378 for four inherently contradictory inputs: `bold` style with `normal` or `400` weight (style says bold, weight says normal), `normal` style with `italic` weight, and `bold` style with `italic` weight. These pairs cannot describe any real font face, so the library refuses to silently pick one. It is reached via `API.setFont` (src/jspdf.js:4937) and `API.addFont` (src/jspdf.js:5018).

Source

Thrown at src/jspdf.js:378

  /**
   * @function combineFontStyleAndFontWeight
   * @param {string} fontStyle Fontstyle or variant. Example: "italic".
   * @param {number | string} fontWeight Weight of the Font. Example: "normal" | 400
   * @returns {string}
   * @private
   */
  var combineFontStyleAndFontWeight = (API.__private__.combineFontStyleAndFontWeight = function(
    fontStyle,
    fontWeight
  ) {
    if (
      (fontStyle == "bold" && fontWeight == "normal") ||
      (fontStyle == "bold" && fontWeight == 400) ||
      (fontStyle == "normal" && fontWeight == "italic") ||
      (fontStyle == "bold" && fontWeight == "italic")
    ) {
      throw new Error("Invalid Combination of fontweight and fontstyle");
    }
    if (fontWeight) {
      fontStyle =
        fontWeight == 400 || fontWeight === "normal"
          ? fontStyle === "italic"
            ? "italic"
            : "normal"
          : (fontWeight == 700 || fontWeight === "bold") &&
            fontStyle === "normal"
          ? "bold"
          : (fontWeight == 700 ? "bold" : fontWeight) + "" + fontStyle;
    }
    return fontStyle;
  });

  /**
   * @callback ApiSwitchBody
   * @param {jsPDF} pdf

View on GitHub (pinned to a3930ce03a)

Solutions

  1. Pass a consistent pair: use fontStyle for the variant (`'normal'`, `'italic'`) and fontWeight for the weight (`400`/`'normal'`, `700`/`'bold'`).
  2. If you want bold, set `fontStyle='normal'` (or `'italic'`) and `fontWeight=700`/`'bold'` — do not also set fontStyle to `'bold'`.
  3. For italic bold, use `fontStyle='italic'` with `fontWeight=700`.
  4. Double-check positional args to `addFont(postScriptName, fontName, fontStyle, fontWeight, encoding)` — a 4th-arg string like `'italic'` is treated as weight, not a second style.

Example fix

// before
 doc.setFont('helvetica', 'bold', 400);   // contradictory

// after
 doc.setFont('helvetica', 'normal', 700);   // bold via weight
 // or italic bold:
 doc.setFont('helvetica', 'italic', 700);
Defensive patterns

Strategy: validation

Validate before calling

var VALID_STYLES = ['normal', 'italic', 'bold'];
var VALID_WEIGHTS = [400, 700, 'normal', 'bold'];
function isValidFontCombo(style, weight) {
  // disallow the four contradictory combos the library rejects
  var bad = (
    (style === 'bold' && (weight === 'normal' || weight === 400)) ||
    (style === 'normal' && weight === 'italic') ||
    (style === 'bold' && weight === 'italic')
  );
  return !bad;
}
if (weight && !isValidFontCombo(fontStyle, fontWeight)) {
  throw new Error('fontStyle and fontWeight conflict');
}

Type guard

function isValidFontCombo(style, weight) {
  if (!weight) return true;
  var s = String(style), w = String(weight).toLowerCase();
  return !(
    (s === 'bold' && (w === 'normal' || w === '400')) ||
    (s === 'normal' && w === 'italic') ||
    (s === 'bold' && w === 'italic')
  );
}

Try / catch

try {
  doc.setFont(name, fontStyle, fontWeight);
} catch (e) {
  if (/Invalid Combination of fontweight and fontstyle/.test(e.message)) {
    // fall back to a non-contradictory pair
    doc.setFont(name, 'normal', 400);
  } else throw e;
}

Prevention

When it happens

Trigger: `doc.setFont('helvetica', 'bold', 400)`; `doc.setFont('helvetica', 'bold', 'normal')`; `doc.setFont('helvetica', 'normal', 'italic')`; `doc.setFont('helvetica', 'bold', 'italic')`; calling `addFont('Helvetica', 'helv', 'bold', 'italic')` where the 4th positional arg is treated as fontWeight (not encoding).

Common situations: Migrating from CSS font shorthand where style and weight come from separate fields and accidentally conflict; passing a combined CSS value like `'italic'` into the weight slot; using fontWeight as a string variant label instead of a numeric/keyword weight; positional-argument mistakes in addFont where the 4th arg is fontWeight but the caller passes a style.

Related errors


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