function mergeDOCX($source_file, $merged_file) { // Important: we get the merge data first, because the phpWord // autoloader included below stuffs up the Jethro autoloader // and causes errors. $data = array_values($this->getMergeData()); // NB THIS FILE HAS BEEN CHANGED! require_once 'include/phpword/src/PhpWord/Autoloader.php'; \PhpOffice\PhpWord\Autoloader::register(); \PhpOffice\PhpWord\Settings::setTempDir(dirname($source_file)); $templateProcessor = new \PhpOffice\PhpWord\TemplateProcessor($source_file); if (!$templateProcessor->cloneBlock('MERGEBLOCK', count($data))) { $vars = $templateProcessor->getVariables(); if (empty($vars)) { trigger_error("You don't seem to have included any \${keywords} in your file; cannot merge"); return; } $templateProcessor->cloneRow(reset($vars), count($data)); } foreach ($data as $num => $row) { foreach ($row as $k => $v) { $templateProcessor->setValue(strtoupper($k) . '#' . ($num + 1), $this->xmlEntities($v)); } } $templateProcessor->saveAs($merged_file); }