A
A
Aricus2018-08-27 09:33:37
PHP
Aricus, 2018-08-27 09:33:37

PhpWord: what's wrong with a template?

You need to create documents according to the template. If you create a document from scratch, everything goes fine, but there is neither saving the file nor, judging by var_dump, replacing by labels. Where is my error?

require_once 'vendor/autoload.php';
$phpWord = new \PhpOffice\PhpWord\PhpWord(); // Подключение PhpWord
$document = $phpWord->loadTemplate('templates\test.docx'); // Загрузка шаблона
$document->setValue('heading', 'Артём'); // Замена меток на значения
$document->setValue('text1', 'солнце');
$document->setValue('text2', 'небо');
var_dump($document);
$document->save('documents\result.docx'); // Сохранение документа

Data from var_dump:
protected]=> string(50) "C:\Users\Artem\AppData\Local\Temp\Php9AF8.tmp" ["tempDocumentMainPart":protected]=> string(2665) " My name is &{heading}!May it always be &{text1}!Let it always be &{text2}!" ["tempDocumentHeaders":protected]=> array(0) { } ["tempDocumentFooters":protected]=> array(0) { } }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Biocoder, 2018-09-06
@Biocoder

Doc: https://phpword.readthedocs.io/en/latest/templates...
Example: https://github.com/PHPOffice/PHPWord/blob/master/s...

require_once 'vendor/autoload.php';

$document = new \PhpOffice\PhpWord\TemplateProcessor('templates\test.docx');

$document->setValue('heading', 'Артём')
$document->setValue('text1', 'солнце');
$document->setValue('text2', 'небо');

$document->saveAs('documents\result.docx');

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question