<?php
//https://redstapler.co/phpword-intro/
//https://stackoverflow.com/questions/30728663/how-to-edit-headers-footers-from-a-document-using-phpword
require_once 'vendor/autoload.php';
$phpWord = new \PhpOffice\PhpWord\PhpWord();
/* $phpWord->getCompatibility()->setOoxmlVersion(14);
$phpWord->getCompatibility()->setOoxmlVersion(15); */
$generatedDocsFolder = "docs/generated/";
$generatedDocName = "headerAndFooter.docx";
$generatedDocPath = $generatedDocsFolder.$generatedDocName;
$section = $phpWord->addSection();
$header = $section->createHeader();//$footer = $section->createFooter();
$fontStyle = new \PhpOffice\PhpWord\Style\Font();
$fontStyle->setBold(true);
$fontStyle->setName('Bookman Old Style');
$fontStyle->setSize(14);
$myTextElement = $section->addText('Hello World');
$myTextElement->setFontStyle($fontStyle);
$headers = $section->getHeaders();//Access to the footer data is very similar: $footers = $section->getFooters();
$header1 = $headers[1]; // note that the first index is 1 here (not 0)
$myHeaderTextElement = $header1->addText('Hello World');
$elements = $header1->getElements();
$element1 = $elements[0]; // and first index is 0 here normally
// for example manipulating simple text information ($element1 is instance of Text object)
$element1->setText("This is my text addition - old part: " . $element1->getText());
/* $sectionStyle = array(
'orientation' => 'landscape',
'marginTop' => 600,
'colsNum' => 2,
); */
$sectionStyle = array(
'pageNumberingStart' => 1,
'marginLeft' => 600,
'marginRight' => 600,
'marginTop' => 600,
'marginBottom' => 600
);
$section = $phpWord->addSection($sectionStyle);
$header2 = $section->createHeader();//$footers = $section->createFooter();
$header2->headerTop(10);
$header2->addText('Header 2');
$header2->addText(htmlspecialchars('Page :'));
$header2->addField('PAGE', array('format' => 'ArabicDash'));
$header2->addPreserveText('Page {PAGE} of {NUMPAGES}', null, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
$section->addText(htmlspecialchars('Date field:'), null, array('alignment' => \PhpOffice\PhpWord\SimpleType\Jc::CENTER));
$section->addText(htmlspecialchars(' of '));
$section->addField('NUMPAGES', array('format' => 'Arabic', 'numformat' => '0,00'), array('PreserveFormat'));
$footer = $section->createFooter();
$footer->addText('Footer 2');
$section->addPageBreak();
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($generatedDocPath);
?>