Forum


Replies: 2   Views: 956
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 pedern  · 27-09-2022 - 08:12

Hi

I wonder how I can add a header and a footer from to docx that is html

I found this example:

$docx= new CreateDocx();


 

$html= '<p style="border: 2px solid black; color: red">A short text with a red border.</p>';


 

$rawWordML = $docx->embedHTML($html, array('rawWordML' => true, 'target' => 'defaultHeader'));

$html = $docx->createWordMLFragment(array($rawWordML));

$docx->addHeader(array('default' => $html));


 

$docx->createDocx('example_embedSimpleHTML');

but I got an php error:

Fatal error: Uncaught Error: Call to undefined method CreateDocx::createWordMLFragment()

This example was from 2014 som maybe it has been replaced???

Best Peder

 

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.

Posted by pedern  · 27-09-2022 - 08:43

Thanks this works as I wanted