0

I am trying to create a Word doc in php (7.4) and am using PhpWord 0.18.2. All goes fine right up until I save the document. The system throws an HTTP 500 (Internal sever error). I have searched for this but could not find anything on it. Googleverse and StackOverflow keep pointing me to the follow (or similar) code and none of the results deals with my issue (that I could find). Here it is:

<?php
echo(__FILE__ . ' ' . __LINE__ . '<br />');
include('../vendor/autoload.php');
echo(__FILE__ . ' ' . __LINE__ . '<br />');
$phpWord = new \PhpOffice\PhpWord\PhpWord();
echo(__FILE__ . ' ' . __LINE__ . '<br />');
// Create the new document..
$phpWord = new \PhpOffice\PhpWord\PhpWord();
echo(__FILE__ . ' ' . __LINE__ . '<br />');
// Add an empty Section to the document
$section = $phpWord->addSection();
echo(__FILE__ . ' ' . __LINE__ . '<br />');
// Add Text element to the Section
$section->addText(
    'Hello World!.'
);
echo(__FILE__ . ' ' . __LINE__ . '<br />');
// Save document
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
echo(__FILE__ . ' ' . __LINE__ . '<br />');
$objWriter->save('HelloWorld.docx');
echo(__FILE__ . ' ' . __LINE__ . '<br />');

I have used the echo statements to determine where the system fails. Here is the output:

/var/www/intra/modules/ViventiumFMLAEligibility/PhpWordTest.php 2
/var/www/intra/modules/ViventiumFMLAEligibility/PhpWordTest.php 4
/var/www/intra/modules/ViventiumFMLAEligibility/PhpWordTest.php 6
/var/www/intra/modules/ViventiumFMLAEligibility/PhpWordTest.php 9
/var/www/intra/modules/ViventiumFMLAEligibility/PhpWordTest.php 12
/var/www/intra/modules/ViventiumFMLAEligibility/PhpWordTest.php 17
/var/www/intra/modules/ViventiumFMLAEligibility/PhpWordTest.php 20

NOTE: I have tried it without the echo statements (my search mentioned that the file will be corrupt with any echo/print_r statements). In both cases, it throws the 500 error.

This has to be something simple, but I just can't see what the issue is. The console isn't much help. Any advice on how to troubleshoot 500 errors is welcome. I'm no sys admin, so a little hand-holding would be appreciated.

dneimeier
  • 89
  • 2
  • 12

1 Answers1

0

For those who are noobs, like me:

more /var/log/apache2/error.log

Will list the errors. Mine is as follows:

[Sun Jan 30 14:43:33.064102 2022] [:error] [pid 17311] [client 10.10.220.32:64626] PHP Fatal error:  Uncaught Error: Class 'ZipArchive' not found in /var/www/intra
/modules/vendor/phpoffice/phpword/src/PhpWord/Shared/ZipArchive.php:135\nStack trace:\n#0 /var/www/intra/modules/vendor/phpoffice/phpword/src/PhpWord/Writer/Abstra
ctWriter.php(285): PhpOffice\\PhpWord\\Shared\\ZipArchive->open('HelloWorld.docx', 8)\n#1 /var/www/intra/modules/vendor/phpoffice/phpword/src/PhpWord/Writer/Word20
07.php(99): PhpOffice\\PhpWord\\Writer\\AbstractWriter->getZipArchive('HelloWorld.docx')\n#2 /var/www/intra/modules/ViventiumFMLAEligibility/PhpWordTest.php(21): P
hpOffice\\PhpWord\\Writer\\Word2007->save('HelloWorld.docx')\n#3 {main}\n  thrown in /var/www/intra/modules/vendor/phpoffice/phpword/src/PhpWord/Shared/ZipArchive.
php on line 135

Looks like I am missing ZipArchive.php in my PhpWord install.

It turns out that the settings file ({path_to_vendor}/phpoffice/phpword/src/PhpWord/Settings.php) needed the ZIPARCHIVE const changed from 'ZipArchive' to 'PhpOffice\\PhpWord\\src\\PhpWord\\Shared\\ZipArchive'. Works like a champ now.

dneimeier
  • 89
  • 2
  • 12