Forum


Replies: 3   Views: 661
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 usman-mani6  · 07-02-2023 - 16:51

Hi Everyone,

I am trying to add footer using embedded html.
Below is the code i am trying but it's not working let me know what i am doing wrong here
 

$docx = new \Phpdocx\Create\CreateDocxFromTemplate($tempFilePath);

                $docx->removeFooters();

                $docs = new \Phpdocx\Create\CreateDocx();

                $htmlFooter = '<footer>

                                <div class="footer">

                                    <table border="1" style="border-collapse: collapse" width="600">

                                        <tbody>

                                            <tr width="600">

                                                <td>Cell A</td>

                                                <td><img src="image.png" width="35" height="35" style="vertical-align: -15px" data-textAlign="left"></td>

                                                <td><phpdocx_pagenumber data-target="defaultFooter" data-type="page-of" data-textAlign="right" /></td>

                                            </tr>

                                        </tbody>

                                    </table>

                                </div>

                            </footer>';

                $footerFragment = new \Phpdocx\Elements\WordFragment($docs, 'defaultFooter');

                // $footerFragment->embedHTML($htmlFooter);

                $footerFragment->embedHTML($htmlFooter, array('useHTMLExtended' => true));

                $docs->addFooter(array('default' => $footerFragment));

                $docs->createDocx($tempFilePath);

Thanks

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.

Posted by usman-mani6  · 08-02-2023 - 08:36

Hi There, 

I have the premium package installed on my server. 
The provided code in the reply doesn't work on the side. I have tried all the possible code in the provided example but none of them work and generated footer on my docs. 
$docx->embedHTML($html, array('useHTMLExtended' => true));

Please help me on this

Posted by admin  · 08-02-2023 - 08:57

Hello,

As explained in our previous all codes are fully tested and working.

If it's not working on your side, then some change must be being done in your code or you are not using the correct license or phpdocx version.

For futher support, please send an email to contact[at]phpdocx.com

Regards.