Forum


Replies: 3   Views: 621
Add footers when importing 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  · 07-02-2023 - 20:03

Hello,

phpdocx allows adding footer from HTML using the following approaches:

  • Using HTML Extended. Only available in Premium licenses.
  • Using WordFragments. Available in all licenses.

For example, using HTML Extended with the default phpdocx_footer tag (only Premium licenses):

$docx = new CreateDocx();

$html = '
    <phpdocx_footer>
        <div class="footer">
            <table border="1" style="border-collapse: collapse" width="600">
                <tbody>
                    <tr width="600">
                        <td>Cell A</td>
                        <td><phpdocx_pagenumber data-target="defaultFooter" data-type="page-of" data-textAlign="right" /></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </phpdocx_footer>
';

$docx->embedHTML($html, array('useHTMLExtended' => true));

$docx->createDocx('output');

To use a custom tag, such as footer, it must be set as explained on the documentation pages (only Premium licenses):

$docx = new CreateDocx();

HTMLExtended::$tagsBlock['footer'] = 'addFooter';

$html = '
    <footer>
        <div class="footer">
            <table border="1" style="border-collapse: collapse" width="600">
                <tbody>
                    <tr width="600">
                        <td>Cell A</td>
                        <td><phpdocx_pagenumber data-target="defaultFooter" data-type="page-of" data-textAlign="right" /></td>
                    </tr>
                </tbody>
            </table>
        </div>
    </footer>
';

$docx->embedHTML($html, array('useHTMLExtended' => true));

$docx->createDocx('output');

Example using a WordFragment (all licenses):

$docx = new CreateDocx();

$html = '<div class="footer">
<table border="1" style="border-collapse: collapse" width="600">
    <tbody>
        <tr width="600">
            <td>Cell A</td>
            <td>Cell B</td>
        </tr>
    </tbody>
</table>
</div>';

$footerHtml = new WordFragment($docx, 'defaultFooter');
$footerHtml->embedHtml($html);

$docx->addFooter(array('default' => $footerHtml));

$docx->createDocx('output');

Example using a WordFragment and HTML Extended (only Premium licenses):

$docx = new CreateDocx();

$html = '<div class="footer">
<table border="1" style="border-collapse: collapse" width="600">
    <tbody>
        <tr width="600">
            <td>Cell A</td>
            <td><phpdocx_pagenumber data-target="defaultFooter" data-type="page-of" data-textAlign="right" /></td>
        </tr>
    </tbody>
</table>
</div>';

$footerHtml = new WordFragment($docx, 'defaultFooter');
$footerHtml->embedHtml($html, array('useHTMLExtended' => true));

$docx->addFooter(array('default' => $footerHtml));

$docx->createDocx('output');

All codes are fully tested and working. You can find similar samples in the package. The same can be used to generate headers using the corresponding methods and tags.

Regards.