PHPOffice/PHPWord · error · Exception

" " is not a valid writer.

Error message

"{$name}" is not a valid writer.

What it means

IOFactory::createWriter() only allows the writer names 'ODText', 'RTF', 'Word2007', 'HTML', 'PDF', 'EPub3' (plus the internal 'WriterInterface' marker). Any other $name throws a PhpWord Exception before attempting to instantiate PhpOffice\PhpWord\Writer\{$name}. 'PDF' resolves to the PDF writer wrapper, which itself requires an underlying PDF library.

Solutions

  1. Use one of the exact names: 'Word2007' (for .docx), 'ODText', 'RTF', 'HTML', 'PDF', 'EPub3'.
  2. Mind case sensitivity — 'word2007' is rejected; use 'Word2007'.
  3. Omit the $name argument to default to 'Word2007'.
  4. If using 'PDF', install/configure a supported PDF library (e.g. dompdf/tcpdf) or write Word2007 and convert externally.

Example fix

// before
$writer = IOFactory::createWriter($phpWord, 'DOCX');
// after
$writer = IOFactory::createWriter($phpWord, 'Word2007');
Defensive patterns

Strategy: validation

Validate before calling

$writers = ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF', 'EPub3'];
if (!in_array($name, $writers, true)) { $name = 'Word2007'; }

Type guard

function isValidWriterName(string $name): bool { return in_array($name, ['ODText','RTF','Word2007','HTML','PDF','EPub3'], true); }

Try / catch

try { $writer = IOFactory::createWriter($phpWord, $name); } catch (\PhpOffice\PhpWord\Exception\Exception $e) { $writer = IOFactory::createWriter($phpWord, 'Word2007'); }

Prevention

When it happens

Trigger: IOFactory::createWriter($phpWord, 'DOCX'), createWriter($phpWord, 'word2007') (case-sensitive), or passing a writer class FQCN instead of the short name.

Common situations: Case mismatches ('Word2007' vs 'word2007'), using format extensions as names ('docx'), expecting 'PDF' to work without a configured PDF renderer (DomPDF/TCPDF), or older/newer PhpWord versions with a different writer list.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of PHPOffice/PHPWord@aef95c0415 (2026-09-14). Data as JSON: /api/errors/2828bfc10db2ab5e. Report an issue: GitHub.

Appendix: source

Thrown at src/PhpWord/IOFactory.php:40

use PhpOffice\PhpWord\Element\TextRun;
use PhpOffice\PhpWord\Exception\Exception;
use PhpOffice\PhpWord\Reader\ReaderInterface;
use PhpOffice\PhpWord\Writer\WriterInterface;
use ReflectionClass;

abstract class IOFactory
{
    /**
     * Create new writer.
     *
     * @param string $name
     *
     * @return WriterInterface
     */
    public static function createWriter(PhpWord $phpWord, $name = 'Word2007')
    {
        if ($name !== 'WriterInterface' && !in_array($name, ['ODText', 'RTF', 'Word2007', 'HTML', 'PDF', 'EPub3'], true)) {
            throw new Exception("\"{$name}\" is not a valid writer.");
        }

        $fqName = "PhpOffice\\PhpWord\\Writer\\{$name}";

        return new $fqName($phpWord);
    }

    /**
     * Create new reader.
     *
     * @param string $name
     *
     * @return ReaderInterface
     */
    public static function createReader($name = 'Word2007')
    {
        return self::createObject('Reader', $name);
    }

View on GitHub (pinned to aef95c0415)