Search code examples
positionfootermarginsphpwordphpoffice

PHPWord Footer Position


Does anyone know if it is possible to re-position (or set the height of) a footer using PHPWord?

I have a footer exactly as required in terms of text.

$footer = $section->addFooter();
$textrun = $footer->addTextRun();
$textrun->addText('My Footer Text');

However, what I'd like to achieve is to:

Reduce the height of the footer or set the distance from bottom of the page.

Space I'd like to remove

There in an option in Word365 called "Footer from bottom", there are also similar options in older versions of Word.

Option in Word365

I've tried adjusting the page margins but these appear to be separate from the footer (and header) positioning.


Solution

  • I managed to find a solution by reviewing the GitHub repo.

    This commit provides a solution: Added support for page header & page footer height

    You can pass the attributes "headerHeight" & "footerHeight" when you create the section that contains your header and footer.

    // Adding an empty Section to the document...
    $section = $this->_phpWord->addSection(array(
                            'headerHeight' => 300,
                            'footerHeight' => 50)
                        );
    
    $footer = $section->addFooter();
    $textrun = $footer->addTextRun();
    $textrun->addText('My Footer Text');
    

    There are also some public methods for setting these values after you've created your section, these are: setFooterHeight() and setHeaderHeight().

    $section->setHeaderHeight(300);
    $section->setFooterHeight(50);