Forum


Replies: 3   Views: 1047
Section headers
Topic closed:
Please note this is an old forum thread. Information in this post may be out-to-date and/or erroneous.
Every phpdocx version includes new features and improvements. Previously unsupported features may have been added to newer releases, or past issues may have been corrected.
We encourage you to download the current phpdocx version and check the Documentation available.

Posted by admin  · 11-06-2021 - 05:16

Hello,

Using addHeader and addFooter methods you can set first, default and even header and footer contents when creating a single section in the DOCX.

For example, to add to the existing section an empty header in the first page and a default header in the next pages:

$docx = new CreateDocx();

$headerFragment_first = new WordFragment($docx, 'firstHeader');
$headerFragment_first->addText('');

$html = '<p>HTML content</p>';
$headerFragment_default = new WordFragment($docx, 'defaultHeader');
$headerFragment_default->embedHTML($html);

$docx->addHeader(
        array(
                'first' => $headerFragment_first,
                'default' => $headerFragment_default,
        )
);

$docx->addText('Text first page');
$docx->addBreak(array('type' => 'page'));
$docx->addText('Text second page');

$docx->createDocx('output');

This works for documents with a single section. As you are doing now, to generate documents with multiple sections with their own headers and footers, you need to create a DOCX for each section and them merge them using mergeDocx.

Regards.