Forum


Replies: 1   Views: 1283
Working example on html_extended footer?
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 pwctechnicalsecurity  · 18-12-2019 - 08:48

Hi guys,

I am trying to sort out my Footer, but I cannot seem to find in the documentation what I need.

First question:

$html = '
    <phpdocx_footer>
        <phpdocx_pagenumber data-target="defaultFooter" data-type="numerical" data-fontSize="10" /><phpdocx_text data-target="defaultFooter" data-text=" | " data-fontSize="10" /><phpdocx_simplefield data-target="defaultFooter" data-fieldName="title" data-fontSize="10" />
    </phpdocx_footer>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$docx->createDocx('html.docx');

From a content-perspective, this seems to work. However, it renders as:

1
|
TITLE

whereas I would like to display it as:

1  |  TITLE

Any suggestions? I basically need something similar as the snippet below (which does work):

        $imageOptions = array(
            'src' => $this->getParameter('kernel.project_dir') . '/public/images/footer.png',
            'height' => 163,
            'width' =>  215,
            'relativeToHorizontal' => 'page',
            'relativeToVertical' => 'page',
            'horizontalOffset' => 459740,
            'verticalOffset' => 9558020,
            'textWrap' => 3
        );

        $footer = new WordFragment($toc, 'defaultFooter');

        $footerImage = new WordFragment($toc, 'defaultFooter');
        $footerImage->addImage($imageOptions);

        $pageNumber = new WordFragment($toc, 'defaultFooter');
        $pageNumber->addPageNumber('numerical');

        $divider = new WordFragment($toc, 'defaultFooter');
        $divider->addText('  |  ');

        $docTitle = new WordFragment($toc, 'defaultFooter');
        $docTitle->addSimpleField('title');

        $text = array();
        $text[] = $footerImage;
        $text[] = $pageNumber;
        $text[] = $divider;
        $text[] = $docTitle;

        $footer->addText($text);

        $toc->addFooter(array('default' => $footer));

Second, related question:

if I try to add an image in the footer, it seems not to work. I have obtained the sample snippet in /Core/EmbedHTML/sample_6.php. There, the reference is included: 

<phpdocx_image data-src="../../img/image.png" data-imageAlign="center" data-scaling="50" />

In a regular inclusion, this works. However, if I copy this exact same code into my HTML Extended snippet, so that it reads the following, it does not work.

$html = '
    <phpdocx_footer>
        <phpdocx_pagenumber data-target="defaultFooter" data-type="numerical" data-fontSize="10" /><phpdocx_text data-target="defaultFooter" data-text=" | " data-fontSize="10" /><phpdocx_simplefield data-target="defaultFooter" data-fieldName="title" data-fontSize="10" />
<phpdocx_image data-src="../../img/image.png" data-imageAlign="center" data-scaling="50" />
    </phpdocx_footer>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

$docx->createDocx('html.docx');

Basically, I am aiming to translate the working snippet above to a HTML Extended version.
Thank you for your help and suggestions!

Posted by admin  · 18-12-2019 - 10:27

Hello,

phpdocx_text and other tags are added as block content; this is the same than using addText standalone. But this tag doesn't replicate the behaviour of:

$text = array();
$text[] = $footerImage;
$text[] = $pageNumber;
$text[] = $divider;
$text[] = $docTitle;

This is adding several elements into a single paragraph as inline contents.

Using HTMLExtended, we recommend you to add the contents using a table, as the included example Core/embedHTML/sample_7.php:

$html = '
    <head>
        <style>
            p {font-style: italic;}
            p.header {font-size: 20px; font-weight: bold;}
            div.footer {font-size: 10px;}
        </style>
    </head>
    <phpdocx_header data-type="default">
        <p class="header">
            Custom header <strong>with strong style</strong>
            <phpdocx_link data-text="External link" data-url="http://www.google.es" />
        </p>
    </phpdocx_header>
    <p>Lorem ipsum dolor sit amet.</p>
    <phpdocx_footer>
        <div class="footer">
            <table border="1" style="border-collapse: collapse" width="600">
                <tbody>
                    <tr width="600">
                        <td>Cell A</td>
                        <td><img src="../../img/image.png" width="35" height="35" style="vertical-align: -15px"></p></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));

This same sample adds an image to the footer and the output is correctly. Please run it and check the output; for footers and headers please use img tag instead of phpdocx_image.
The next version of HTML Extended will include support to set a target when adding contents to footers and headers (as phpdocx_image needs).

Regards.