Forum


Replies: 2   Views: 2464
Use addlist with embedded html containing a list.
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 plintsites  · 25-01-2019 - 16:08

Hello!

I am trying to make a word document with a nested list. The contents of sublist items are HTML, coming from a wysiwyg editor. Hence, this content may contain lists (ul's and/or ol's) also. It seems that this content does not load correctly.

To elaborate a bit on the subject I tweaked the addList example #3 from the docs a little, see below

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

$textData = new \Phpdocx\Elements\WordFragment($docx);
        $text = array();
        $text[] = array('text' => 'We insert some ');
        $text[] = array('text' => 'bold text', 'b' => 'on');
        $textData->addText($text);

        // also, some simple HTML to illustrate the fexibility of the method
        $htmlData = new \Phpdocx\Elements\WordFragment($docx);
        $html = '<i>Some HTML code</i> with a <a href="http://www.phpdocx.com">link</a>';
        $htmlData->embedHTML($html);

        // And some more complex HTML containing a list itself
        $htmlList = new \Phpdocx\Elements\WordFragment($docx);
        $htmlList->embedHTML('<ul>
                                <li>football</li>
                                <li>athletics
                                    <ul>
                                        <li>javelin</li>
                                        <li>highjump</li>
                                        <li>100 m</li>
                                        <li>marathon</li>
                                    </ul>
                                </li>
                                <li>cycling
                                    <ul>
                                        <li>track</li>
                                        <li>cyclocross</li>
                                        <li>mountainbike</li>
                                        <li>bmx</li>
                                        <li>road cycling</li>
                                    </ul>
                                </li>
                                <li>tennis</li>
                            </ul>');

        $itemList= array(
            'In this example we use a custom list (val = 5) that comes bundled with the default phpdocx template.',
            array(
                $textData,
                'Line B',
                'Line C'
            ),
            $htmlData,
            'Line 3',
            'And now some more complex HTML using an unordered list...',
            $htmlList
        );

        // Add list to doc
        $docx->addList($itemList, 2);

In the resulting word document, the content of the $htmlList variable is not shown as a list.

I am very curious if it is possible to achieve what I am describing here.

 

With kind regards!

Posted by admin  · 25-01-2019 - 16:14

Hello,

No, you can't add a block content such as a list or a table as a list element (from HTML or as WordFragment), as it's transformed as inline content.

Regards.

Posted by IHD  · 27-01-2019 - 16:50

Hi, 

I suggest you parse the HTML with the PHP DOM tools, and from there build the additional array items. Please refer to https://stackoverflow.com/questions/13553572/php-dom-get-items-from-first-ul-element or google "php dom ul list" or similar. 

Good luck!