PHPOffice/PHPWord · error · InvalidStyleException
Line height must be a valid number
Error message
Line height must be a valid number
What it means
PhpWord\Style\Paragraph::setLineHeight accepts only a positive int/float line-height. Strings are stripped of non-numeric characters and cast to float; if the resulting value is not numeric or is falsy (0, 0.0, empty after stripping), InvalidStyleException('Line height must be a valid number') is thrown.
Solutions
- Pass a plain number, e.g. setLineHeight(1.5) for 150% spacing
- Pre-clean the string yourself and verify it is > 0 before calling
- For 'auto' spacing, use spacingLineRule AUTO via setSpacingLineRule instead of setLineHeight
Example fix
// before
$paragraph->setLineHeight('1.5em'); // may strip to 1.5 OK, but 'auto' throws
// after
$paragraph->setLineHeight(1.5); Defensive patterns
Strategy: type-guard
Validate before calling
function assertLineHeight($v): float {
$f = is_string($v) ? (float)preg_replace('/[^0-9\.]/', '', $v) : (float)$v;
if ($f <= 0) throw new InvalidArgumentException('lineHeight must be a positive number');
return $f;
}
$paragraph->setLineHeight(assertLineHeight($config['lineHeight'])); Type guard
function isNumericLineHeight($v): bool {
return (is_int($v) || is_float($v)) && $v > 0;
} Try / catch
try {
$paragraph->setLineHeight($value);
} catch (\PhpOffice\PhpWord\Exception\InvalidStyleException $e) {
$paragraph->setLineHeight(1.0); // single spacing default
} Prevention
- Pass numeric values, not CSS strings like '1.5em' or 'auto'
- Cast user config with (float) and validate > 0 before setting
- Replace 'auto' behavior with setSpacingLineRule(LineSpacingRule::AUTO)
When it happens
Trigger: setLineHeight('abc'), setLineHeight(''), setLineHeight(0), or a string containing no digits (e.g. 'px', '1.5x' is OK because 1.5 survives stripping but 'auto' becomes 0 and throws).
Common situations: Passing CSS-like values ('1.5em', 'auto') or localized numbers with commas stripped to 0; user-supplied config with a non-numeric line-height; confusion with the CSS line-height property.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- Invalid style value: Options
- Invalid style value
- Invalid image
- salt has to be of exactly 16 bytes length
- Type must be "start" or "end"
AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14).
Data as JSON: /api/errors/2456efe4a2bc951f.
Report an issue: GitHub.
Appendix: source
Thrown at src/PhpWord/Style/Paragraph.php:599
{
return $this->lineHeight;
}
/**
* Set the line height.
*
* @param float|int|string $lineHeight
*
* @return self
*/
public function setLineHeight($lineHeight)
{
if (is_string($lineHeight)) {
$lineHeight = (float) (preg_replace('/[^0-9\.\,]/', '', $lineHeight));
}
if ((!is_int($lineHeight) && !is_float($lineHeight)) || !$lineHeight) {
throw new InvalidStyleException('Line height must be a valid number');
}
$this->lineHeight = $lineHeight;
$this->setSpacing(($lineHeight - 1) * self::LINE_HEIGHT);
$this->setSpacingLineRule(\PhpOffice\PhpWord\SimpleType\LineSpacingRule::AUTO);
return $this;
}
/**
* Get allow first/last line to display on a separate page setting.
*
* @return bool
*/
public function hasWidowControl()
{
return $this->widowControl;
}View on GitHub (pinned to aef95c0415)