PhpWord add footer to template

1.4k Views Asked by At

I want to add footer to my created document, how can i do this with phpword?

My template processing code:

$template = new \PhpOffice\PhpWord\TemplateProcessor("my-template.docx");

$table = new \PhpOffice\PhpWord\Element\Table();

$table->addRow();
$table->addCell()->addText("test");

$template->setComplexBlock('table_var', $table);

$template->saveAs("new-file.docx");
1

There are 1 best solutions below

6
On

As mentioned in another post, first save your template, and then open the result again like:

$file = \PhpOffice\PhpWord\IOFactory::load("new-file.docx");

// Get Sections from the imported document...
$sections = $file->getSections();
$section = $sections[0];

Then, if your template does not have any footer, add one like:

$footer1 = $section->createFooter();
$footer1->addImage('images/footer.png');

Or, you can edit existing footer like:

// Note that the first index is 1 here (not 0).
$footers = $section->getFooters();
$footer1 = $footers[1];

// And first index is 0 for elements normally.
$elements = $footer1->getElements();
$element1 = $elements[0];

// For example manipulating simple text information ($element1 is instance of Text object)
$element1->setText("My new text, old content: " . $element1->getText());