A
A
Aricus2018-08-24 15:06:54
PHP
Aricus, 2018-08-24 15:06:54

How to connect PhpWord (Class 'PHPWord' not found)?

Downloaded PhpWord via composer:
D:\xampp\htdocs\control>composer require phpoffice/phpword It
downloaded successfully (at least without errors, files and folders appeared. Version - 0.15.0). But I don't understand how to run it in a script. For example, PhpSpreadsheet runs successfully like this:

require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

What should be written instead to run PhpWord? Focusing on information from different sources, I tried different options. The result is always the same (unless there was an error in earlier sections of the code):
Fatal error: Uncaught Error: Class 'PHPWord' not found in D:\xampp\htdocs\control\testtemplate.php:35 Stack trace: #0 {main} thrown in D:\xampp\htdocs\control\testtemplate.php on line 35
I've tried the following:
require_once 'vendor/autoload.php';
use PhpOffice\PhpWord\Word;
use PhpOffice\PhpWord\Writer\Docx;

require_once 'vendor/autoload.php';
\PhpOffice\PhpWord\Autoloader::register();

require_once 'vendor\phpoffice\phpword\src\PhpWord\PhpWord.php';

require_once 'vendor\phpoffice\phpword\bootstrap.php';

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Novikov, 2018-08-24
@Aricus

Well, do you have to use/import the \PhpOffice\PhpWord\PhpWord class?

require_once 'vendor/autoload.php';
$phpWord = new \PhpOffice\PhpWord\PhpWord();

or
require_once 'vendor/autoload.php';
use PhpOffice\PhpWord\PhpWord;

$phpWord = new PhpWord();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question