Forum


Replies: 11   Views: 2819
Handling of lists..
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 coolaustralia  · 22-08-2019 - 22:40

We're having a couple of separate issues, around the same theme.

This HTML code:

<ul><li>Question 1</li</ul>

<img src="blank.png" style="height: 120px" />

<ul><li>Question 2</li></ul>

<img src="blank.png" style="height: 120px" />

 

Results in a word document where the image is above the <li> so there is a break between the bullet and the text.

Alternatively we do this:

<ul><li>Question 1<br /><br /><br /><br /></li><li>Question 2<br /><br /><br /><br /></li></ul>

this results in the breaks being placed ABOVE the text, instead of below.

What we're trying to achieve is a blank space between each <li> of 3-4 lines, so when the document is printed there is space to write.

We're running the latest available PHPDocx

Posted by admin  · 23-08-2019 - 07:20

Hello,

We have run the image approach, and after closing the first li tag correctly:

$html = '
    <ul><li>Question 1</li></ul>
    <img src="image.png" style="height: 120px" />
    <ul><li>Question 2</li></ul>
    <img src="image.png" style="height: 120px" />
';
$docx->embedHTML($html);

The image is added fine after each list item.

Anyway, the easiest approaches to accomplish that task are:

  • Adding br tags out of each list item:
$html = '
    <ul>
        <li>Question 1</li>
        <br /><br /><br /><br />
        <li>Question 2</li>
        <br /><br /><br /><br />
    </ul>
';
$docx->embedHTML($html);
  • Using a CSS style:
$html = '
    <ul>
        <li style="margin-bottom: 100px;">Question 1</li>
        <li>Question 2</li>
    </ul>
';
$docx->embedHTML($html);

Or you can use a custom numbering style.

Regards.

Posted by coolaustralia  · 25-08-2019 - 02:32

Hello,

Thank you for your reply and investigation.

The following html code (as generated)

<ul class="textbox-list"><li>Have you heard of this animal before?</li><br><br><br><br><br><br>

<li>What do you already know about this animal?</li><br><br><br><br><br><br>

</ul>

 

Results in this: https://i.imgur.com/HMyMhc4.png

 

Posted by admin  · 25-08-2019 - 09:30

Hello,

We are running the following script:

<?php

require_once 'classes/CreateDocx.php';

$docx = new CreateDocx();

$html = '
<ul class="textbox-list">
    <li>Have you heard of this animal before?</li>
    <br><br><br><br><br><br>
    <li>What do you already know about this animal?</li>
    <br><br><br><br><br><br>
</ul>
';
$docx->embedHTML($html);

$docx->createDocx('output');

with the latest release of phpdocx (the same version you are using) and the output is correct:

https://i.imgur.com/h3JFaNr.jpg

We have also tested it with phpdocx 8 and phpdocx 7 and the output is the same.

We also see that your HTML has two li tags but the output you get has three li items. Maybe you are adding other HTML or the CSS textbox-list is applying some style or you are enabling any additional option?

Please run the previous script standalone, changing only the path to CreateDocx.

Did you also try the other approach (the margin-bottom style)?

Regards.

Posted by coolaustralia  · 25-08-2019 - 19:59

The extra bullet was from HTML I did not provide, which repeats the same as I provided.

Margin-bottom seems to work but does not interact with page breaks properly, the bullet may end up as the last line on a page, with the space at the top of the next page which is confusing when printed.

I will test your code sample, thank you.

Posted by coolaustralia  · 11-09-2019 - 01:21

We are having issues with adding a margin and page breaks. If the margin flows over a page break (that occurs naturally in the document) the bullet may end up at the very bottom of the page which results in the space from the margin being lost.

Posted by admin  · 11-09-2019 - 10:28

Hello,

That's the default behavior of MS Word and can't be changed. The dev team has done some tests using br tags and this approach works perfectly.

Regards.

Posted by coolaustralia  · 26-09-2019 - 08:43

I ran this code that you provided:

require_once 'Classes/Phpdocx/Create/CreateDocx.php';

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

$html = '
<ul class="textbox-list">
    <li>Have you heard of this animal before?</li>
    <br><br><br><br><br><br>
    <li>What do you already know about this animal?</li>
    <br><br><br><br><br><br>
</ul>
';
$docx->embedHTML($html);

$docx->createDocx('output');

And I see this:

https://imgur.com/sS3Nqku

Posted by admin  · 26-09-2019 - 09:22

Hello,

We get the following output:

https://i.imgur.com/h3JFaNr.jpg

When running the script using classic and namespaces packages.

What version of PHP are you using? It could be a PHP Tidy problem.

phpdocx 9.0 premium licenses include HTML Extended to this and other specific uses but we are searching a solution to be used with your Advanced license.

Regards.

Posted by admin  · 26-09-2019 - 10:21

Hello,

The problem seems a Tidy problem that is adding extra tags (https://stackoverflow.com/questions/8533204/prevent-html-tidy-adding-li-elements), although testing for example using PHP 7.3 no extra LI tag is added and the output is perfect.

Using a Premium license and HTML Extended it can be easily done:

$html = '
<ul class="textbox-list">
    <li>Have you heard of this animal before?</li>
    <phpdocx_break data-type="line" data-number="5" />
    <li>What do you already know about this animal?</li>
    <phpdocx_break data-type="line" data-number="5" />
</ul>
';
$docx->embedHTML($html, array('useHTMLExtended' => true));

But as you are using an Advanced license, you could try updating to PHP 7.3 to check if the Tidy issue is solved automatically, or use an alternative approach compatible with all PHP versions:

$html = '
<ul class="textbox-list">
    <li>Have you heard of this animal before?$VAR_BREAK$$VAR_BREAK$$VAR_BREAK$$VAR_BREAK$$VAR_BREAK$</li>
    <li>What do you already know about this animal?$VAR_BREAK$$VAR_BREAK$$VAR_BREAK$$VAR_BREAK$$VAR_BREAK$</li>
</ul>
';
$docx->embedHTML($html);
$docx->createDocx('output_t');

$docxT = new Phpdocx\Create\CreateDocxFromTemplate('output_t.docx');
$wordFragment = new Phpdocx\Elements\WordFragment($docx);
$wordFragment->addBreak();
$docxT->replaceVariableByWordFragment(array('VAR_BREAK' => $wordFragment), array('type' => 'inline'));
$docxT->createDocx('output.docx');

This code adds VAR_BREAK placeholders and then open the output as template to replace those placeholders by line breaks. You could also use DOCXPath [https://www.phpdocx.com/api-documentation/docx-path/replace-elements-in-docx] to avoid generating a template and do all steps using only the CreateDocx class.

Regards.

Posted by coolaustralia  · 26-09-2019 - 10:36

Hello,

Thanks to your mention of PHP versions, I tested with PHP 7.2 and it works. We were using PHP 7.3

The issue we had with margins is happening with breaks as well, the bullet may end up at the very end of a page, with the blank spaces at the top of the next page. Is this also the Word behaviour that can't be changed?

Posted by admin  · 26-09-2019 - 10:49

Hello,

There's no option to avoid that MS Word behavior. Line breaks can't be hidden automatically based on content positions.

You could use keepNext (page-break-after CSS) and keepLines (page-break-inside CSS) options to keep contents on the same page, so you could avoid that extra line breaks at the top of a page as the list item and line breaks will always be in the same page.

Regards.