@Aricus

PhpWord: в чём ошибка при работе с шаблоном?

Нужно создавать документы по шаблону. Если создавать документ на пустом месте, всё проходит нормально, но тут не происходит ни сохранения файла, ни, судя по var_dump, замены по меткам. Где у меня ошибка?
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'); // Сохранение документа

Данные из var_dump:
object(PhpOffice\PhpWord\TemplateProcessor)#13 (5) { ["zipClass":protected]=> object(PhpOffice\PhpWord\Shared\ZipArchive)#14 (5) { ["numFiles"]=> int(14) ["filename"]=> string(50) "C:\Users\Артем\AppData\Local\Temp\Php9AF8.tmp" ["tempDir":"PhpOffice\PhpWord\Shared\ZipArchive":private]=> NULL ["zip":"PhpOffice\PhpWord\Shared\ZipArchive":private]=> object(ZipArchive)#15 (5) { ["status"]=> int(9) ["statusSys"]=> int(0) ["numFiles"]=> int(14) ["filename"]=> string(50) "C:\Users\Артем\AppData\Local\Temp\Php9AF8.tmp" ["comment"]=> string(0) "" } ["usePclzip":"PhpOffice\PhpWord\Shared\ZipArchive":private]=> bool(false) } ["tempDocumentFilename":protected]=> string(50) "C:\Users\Артем\AppData\Local\Temp\Php9AF8.tmp" ["tempDocumentMainPart":protected]=> string(2665) " Меня зовут &{heading}!Пусть всегда будет &{text1}!Пусть всегда будет &{text2}!" ["tempDocumentHeaders":protected]=> array(0) { } ["tempDocumentFooters":protected]=> array(0) { } }
  • Вопрос задан
  • 4663 просмотра
Пригласить эксперта
Ответы на вопрос 1
@Biocoder
Дока: https://phpword.readthedocs.io/en/latest/templates...
Пример: 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');
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы