1

I am new to the PHPWord library, and have just now installed it using composer. I want to generate a basic word file. This is my code:

<?php
    // (A) LOAD PHPWORD
    require "vendor/autoload.php";
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection();
    $section->addText(
        '"Learn from yesterday, live for today, hope for tomorrow. '
            . 'The important thing is not to stop questioning." '
            . '(Albert Einstein)'
    );
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    $objWriter->save('helloWorld.docx');
?>

Whenever I execute the code, I get this error:

Fatal error: Uncaught Error: Class "ZipArchive" not found

I have followed every tutorial and have also searched in youtube, but I haven't got anything related to this issue. After digging a bit more, I found this post related to the issue.

I followed the solution according to that post.

    \PhpOffice\PhpWord\Settings::setZipClass(Settings::PCLZIP);
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    $objWriter->save('helloWorld.docx');

Fingers crossed, I executed the code, only to be greeted by a new error now:

Fatal error: Uncaught Error: Class "Settings" not found

Can anyone please help me? I am completely new to this library and don't know how to fix this.

1 Answer 1

1

I ran your code and it ran without errors. This code

<?php
    // (A) LOAD PHPWORD
    require "vendor/autoload.php";
    $phpWord = new \PhpOffice\PhpWord\PhpWord();
    $section = $phpWord->addSection();
    $section->addText(
        '"Learn from yesterday, live for today, hope for tomorrow. '
            . 'The important thing is not to stop questioning." '
            . '(Albert Einstein)'
    );
    $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
    $objWriter->save('helloWorld.docx');
?>

Its generating word file. All OK.


As for the "ZipArchive" error :

Please note following dependencies are required for this lib and "zip" is one of them

dependencies: PHPWord requires the following:

PHP 7.1+

Zip extension ( used to write OOXML and ODF)

GD extension (used to add images)

XMLWriter extension (used to write OOXML and ODF)

XSL extension (optional, used to apply XSL style sheet to template )

dompdf library (optional, used to write PDF)


Suggestion: Please check if zip extension for the php is installed by either of the two methods .

  1. via terminal/command line by doing php -m
  2. via phpinfo() method.

if you are using xampp/wamp you need to un-comment extension=php_zip.dll in php.ini ......

Hope it helps....

source: PHPWord Documentation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.