Hi everyone, I hope you all be fine.I am having some trouble using this extension on my Yii2 Application.
Here is the error log:
````
Class 'ZipArchive' not found
in /home/ahmednawaz/Projects/ProjectName/vendor/phpoffice/phpword/src/PhpWord/Shared/ZipArchive.php at line 134
* @param int $flags The mode to use to open the archive
* @return bool
*/
public function open($filename, $flags = null)
{
$result = true;
$this->filename = $filename;
if (!$this->usePclzip) {
$zip = new \ZipArchive();
$result = $zip->open($this->filename, $flags);
// Scrutizer will report the property numFiles does not exist
// See https://github.com/scrutinizer-ci/php-analyzer/issues/190
$this->numFiles = $zip->numFiles;
} else {
$zip = new \PclZip($this->filename);
$this->tempDir = Settings::getTempDir();
$this->numFiles = count($zip->listContent());
````
Here is what I put in my view file
````
use PhpOffice\PhpWord\PhpWord;
$phpWord = new PhpWord();
$section = $phpWord->addSection();
$section->addText(
'"Great achievement is usually born of great sacrifice, '
. 'and is never the result of selfishness." '
. '(Napoleon Hill)',
array('name' => 'Tahoma', 'size' => 10)
);
// Saving the document as OOXML file...
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save('helloWorld.docx');
````
Here is what my controller action looks like
````
$model = $this->findModel($id);
// There is no such thing as rendereing here, I just want to see what happends with phpWord
$this->render('test', ['model' => $model]);
````
May be this could be due to multiple ZipArchive classes (one in the core and the other in the extension. I have personally accessed the open method of phpword/src/shared/zipArchive class and tried to jump to \ZipArchive() and it leads straight to core API method, still I get the error even if the chain-links are correct.
I have no idea what is the issue.
Any help is appreciated.
The issue was resolved. All I had to do was:
\PhpOffice\PhpWord\Settings::setZipClass(Settings::PCLZIP); before processing file
Its working now.
Thanks to:
https://stackoverflow.com/questions/26643775/class-ziparchive-not-found-error-while-using-phpexcel
The issue was resolved. All I had to do was:
\PhpOffice\PhpWord\Settings::setZipClass(Settings::PCLZIP); before processing fileIts working now.
Thanks to:
https://stackoverflow.com/questions/26643775/class-ziparchive-not-found-error-while-using-phpexcel
WORKED LIKE A CHARM. Thank You so much Brother.
It worked for me for PHPWORD.
My Settings :
// importing settings via namespace
use PhpOffice\PhpWord\Settings;
// calling settings right above file generation. (As stated in this answer)
Settings::setZipClass(Settings::PCLZIP);
// Save file
echo write($phpWord, basename(__ FILE __, '.php'), $writers);
// ----------- OR ----------------------------------------
// just paste this part in Sample_Header.php as below.
Settings::loadConfig();
// put below line after loadConfig, as both loadConfig & setZipClass are functions of same settings class.
Settings::setZipClass(Settings::PCLZIP);
Most helpful comment
The issue was resolved. All I had to do was:
\PhpOffice\PhpWord\Settings::setZipClass(Settings::PCLZIP); before processing file
Its working now.
Thanks to:
https://stackoverflow.com/questions/26643775/class-ziparchive-not-found-error-while-using-phpexcel