PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.25k stars 2.69k forks source link

Header/Footer misalignment when spanning sections #2258

Open scottgrobinson opened 2 years ago

scottgrobinson commented 2 years ago

When using a header or footer along with sections that span multiple pages, the header/footer is aligned on page 1, but then misaligns on subsequent pages because it appears to be inserted "inside" the section.

Is this a bug, or is there a way of forceably saying "Stick the header/footer on the base of the page ignoring any sections"

Page 1 - Aligned footer

TestFile - Screenshot Page 1

Page 2 - Footer missaligned and is inside the "section"

TestFile - Screenshot Page 2

Example Document TestFile.docx

Minimal code to replicate

<?php

namespace App\Http\Controllers;

class PlaylistExportController extends Controller
{
    public function generateRandomString($length = 10) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString;
    }

    public function index($type)
    {
        $phpWord = new \PhpOffice\PhpWord\PhpWord();
        $phpWord->getCompatibility()->setOoxmlVersion(15);

        $tableStyle = new \PhpOffice\PhpWord\Style\Table;
        $tableStyle->setUnit(\PhpOffice\PhpWord\Style\Table::WIDTH_PERCENT);
        $tableStyle->setWidth(100 * 50);

        $headerFooterTableStyle = new \PhpOffice\PhpWord\Style\Table;
        $headerFooterTableStyle->setUnit(\PhpOffice\PhpWord\Style\Table::WIDTH_PERCENT);
        $headerFooterTableStyle->setWidth(100 * 50);

        for ($y = 0; $y <= 3; $y++) {
            $section = $phpWord->addSection(['marginLeft' => 300, 'marginRight' => 300, 'marginTop' => 600, 'breakType' => 'continuous']);

            $footer = $section->addFooter();
            $footertable = $footer->addTable($headerFooterTableStyle);
            $footertable->addRow();
            $footertable->addCell(2000)->addPreserveText('{Page {PAGE} of {NUMPAGES}');
            $footertable->addCell(2000)->addText('Generated '.date('d/m/y h:i:s A'));

            $section->addText('Section  - ' . $this->generateRandomString() . '<w:br/>');

            $tablesection = $phpWord->addSection([
                'colsNum' => 2,
                'colsSpace' => 100,
                'breakType' => 'continuous',
            ]);
            $table = $tablesection->addTable();

            for ($x = 0; $x <= 200; $x++) {
                $table->addRow();
                $table->addCell()->addText($this->generateRandomString());
            }
            $tablesection->addPageBreak();
        }

        $phpWord->save('TestFile.docx', 'Word2007', true);
    }
}
github-actions[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If this is still an issue for you, please try to help by debugging it further and sharing your results. Thank you for your contributions.

scottgrobinson commented 2 years ago

I am unable to debug further - Any suggestions or guidance would be more than welcome.