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

  1. Pass a plain number, e.g. setLineHeight(1.5) for 150% spacing
  2. Pre-clean the string yourself and verify it is > 0 before calling
  3. 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

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


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)