Forum


Replies: 2   Views: 952
Add header and footer from html
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  · 27-09-2022 - 08:23

Hello,

That code is from the old phpdocx 3 that is not compatible with the current stable version of phpdocx. The phpdocx API is compatible with all versions since phpdocx 4; new versions add new features, methods, classes and options.

You can add HTML in headers and footers using WordFragments:

require_once 'classes/CreateDocx.php';

$docx = new CreateDocx();

$htmlHeader = '<p style="border: 2px solid black; color: red">A short text with a red border in headers.</p>';
$headerFragment = new WordFragment($docx, 'defaultHeader');
$headerFragment->embedHTML($htmlHeader);
$docx->addHeader(array('default' => $headerFragment));

$htmlFooter = '<p style="border: 2px solid black; color: red">A short text with a red border in footers.</p>';
$footerFragment = new WordFragment($docx, 'defaultFooter');
$footerFragment->embedHTML($htmlFooter);
$docx->addFooter(array('default' => $footerFragment));

$htmlBody = '<p style="font-weight: bold;">Body content.</p>';
$docx->embedHTML($htmlBody);

$docx->createDocx('output');

On the practical guide you can find more information about WordFragments: https://www.phpdocx.com/documentation/practical/wordfragments-and-wordml

You can also generate headers and footers from HTML tags using HTML Extended available in Premium licenses:

https://www.phpdocx.com/htmlapi-documentation/html-extended/insert-header-Word-document-with-HTML

https://www.phpdocx.com/htmlapi-documentation/html-extended/insert-footer-Word-document-with-HTML

Regards.