{"record":{"id":"c97843204e8a8427","repo":"parallax/jsPDF","slug":"invalid-combination-of-fontweight-and-fontstyle","errorCode":null,"errorMessage":"Invalid Combination of fontweight and fontstyle","messagePattern":"Invalid Combination of fontweight and fontstyle","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/jspdf.js","lineNumber":378,"sourceCode":"\n  /**\n   * @function combineFontStyleAndFontWeight\n   * @param {string} fontStyle Fontstyle or variant. Example: \"italic\".\n   * @param {number | string} fontWeight Weight of the Font. Example: \"normal\" | 400\n   * @returns {string}\n   * @private\n   */\n  var combineFontStyleAndFontWeight = (API.__private__.combineFontStyleAndFontWeight = function(\n    fontStyle,\n    fontWeight\n  ) {\n    if (\n      (fontStyle == \"bold\" && fontWeight == \"normal\") ||\n      (fontStyle == \"bold\" && fontWeight == 400) ||\n      (fontStyle == \"normal\" && fontWeight == \"italic\") ||\n      (fontStyle == \"bold\" && fontWeight == \"italic\")\n    ) {\n      throw new Error(\"Invalid Combination of fontweight and fontstyle\");\n    }\n    if (fontWeight) {\n      fontStyle =\n        fontWeight == 400 || fontWeight === \"normal\"\n          ? fontStyle === \"italic\"\n            ? \"italic\"\n            : \"normal\"\n          : (fontWeight == 700 || fontWeight === \"bold\") &&\n            fontStyle === \"normal\"\n          ? \"bold\"\n          : (fontWeight == 700 ? \"bold\" : fontWeight) + \"\" + fontStyle;\n    }\n    return fontStyle;\n  });\n\n  /**\n   * @callback ApiSwitchBody\n   * @param {jsPDF} pdf","sourceCodeStart":360,"sourceCodeEnd":396,"githubUrl":"https://github.com/parallax/jsPDF/blob/a3930ce03a585a26b2c76d12a0f413ce96f6d1a3/src/jspdf.js#L360-L396","documentation":"`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).","triggerScenarios":"`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).","commonSituations":"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.","solutions":["Pass a consistent pair: use fontStyle for the variant (`'normal'`, `'italic'`) and fontWeight for the weight (`400`/`'normal'`, `700`/`'bold'`).","If you want bold, set `fontStyle='normal'` (or `'italic'`) and `fontWeight=700`/`'bold'` — do not also set fontStyle to `'bold'`.","For italic bold, use `fontStyle='italic'` with `fontWeight=700`.","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."],"exampleFix":"// before\n doc.setFont('helvetica', 'bold', 400);   // contradictory\n\n// after\n doc.setFont('helvetica', 'normal', 700);   // bold via weight\n // or italic bold:\n doc.setFont('helvetica', 'italic', 700);","handlingStrategy":"validation","validationCode":"var VALID_STYLES = ['normal', 'italic', 'bold'];\nvar VALID_WEIGHTS = [400, 700, 'normal', 'bold'];\nfunction isValidFontCombo(style, weight) {\n  // disallow the four contradictory combos the library rejects\n  var bad = (\n    (style === 'bold' && (weight === 'normal' || weight === 400)) ||\n    (style === 'normal' && weight === 'italic') ||\n    (style === 'bold' && weight === 'italic')\n  );\n  return !bad;\n}\nif (weight && !isValidFontCombo(fontStyle, fontWeight)) {\n  throw new Error('fontStyle and fontWeight conflict');\n}","typeGuard":"function isValidFontCombo(style, weight) {\n  if (!weight) return true;\n  var s = String(style), w = String(weight).toLowerCase();\n  return !(\n    (s === 'bold' && (w === 'normal' || w === '400')) ||\n    (s === 'normal' && w === 'italic') ||\n    (s === 'bold' && w === 'italic')\n  );\n}","tryCatchPattern":"try {\n  doc.setFont(name, fontStyle, fontWeight);\n} catch (e) {\n  if (/Invalid Combination of fontweight and fontstyle/.test(e.message)) {\n    // fall back to a non-contradictory pair\n    doc.setFont(name, 'normal', 400);\n  } else throw e;\n}","preventionTips":["Express weight numerically (400/700) and keep style to normal/italic.","Never set fontStyle to 'bold' if you also pass a weight — let weight carry boldness.","Centralize font selection behind a helper that validates the pair."],"tags":["font","setfont","fontweight","fontstyle","validation"],"backgroundTag":null,"analyzedSha":"a3930ce03a585a26b2c76d12a0f413ce96f6d1a3","analyzedAt":"2026-08-13T05:33:39.648Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}