PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.16k stars 2.68k forks source link

Exported Word Document Content Issue with List (numbering) #2557

Open marievyyy opened 5 months ago

marievyyy commented 5 months ago

Describe the Bug

When exporting HTML content to a Word document using the PHPWord library, there is an issue with the formatting of lists. The numbering of lists is not displaying correctly, and it appears to be continuous instead of restarting the numbering lists or follow the correct list hierarchy.

Steps to Reproduce

What i have:

My code goes:
$template = new HTMLTemplateProcessor($documentdir.'mytemplate.docx');
// Read content HTML into placeholders
// sample $content value..
<p>&nbsp;Bug Report:</p>
<p><span style="background-color: #ff0000;">BugTracker X</span> is facing an issue when converting HTML content to a Word document. The problem lies in the formatting of lists. The numbering of lists appears to be continuous instead of restarting for nested lists.</p>
<ol>
<li>Items in the first level of the list
<ol>
<li>Sub-item 1</li>
</ol>
</li>
<li>&nbsp;Another item at the first level</li>
<li>Yet another item at the first level
<ol>
<li>Sub-item 1</li>
<li>Sub-item 2</li>
</ol>
<p><span style="background-color: #ff0000;">BugTracker X</span> is facing an issue when converting HTML content to a Word document. The problem lies in the formatting of lists. The numbering of lists appears to be continuous instead of restarting for nested lists.</p>

$template->setHtmlBlockValue('content', "<br/>".$content."<br/>");

Expected Behavior

I expect the HTML to Word document conversion to produce a properly formatted list with correct numbering, especially for nested lists. Each level of the list should have its own numbering, and the numbering should restart appropriately for sub-lists.

Screen Shot 2024-01-25 at 2 41 21 PM

Current Behavior

Screen Shot 2024-01-25 at 2 42 05 PM

Context

oleibman commented 5 months ago

The sample code you supplied is missing one </li> and one </ol> tag in your html. I believe that adding those fixes your problem. You may not be able to set a type (e.g. numeric or alphabetic) for nested lists, but that is a different problem than the one your're reporting.

        $html ='
<p>&nbsp;Bug Report:</p>
<p><span style="background-color: #ff0000;">BugTracker X</span> is facing an issue when converting HTML content to a Word document. The problem lies in the formatting of lists. The numbering of lists appears to be continuous instead of restarting for nested lists.</p>
<ol>
<li>Items in the first level of the list
<ol>
<li>Sub-item 1</li>
</ol>
</li>
<li>&nbsp;Another item at the first level</li>
<li>Yet another item at the first level</li>
<ol>
<li>Sub-item 1</li>
<li>Sub-item 2</li>
</ol>
</ol>
<p><span style="background-color: #ff0000;">BugTracker X</span> is facing an issue when converting HTML content to a Word document. The problem lies in the formatting of lists. The numbering of lists appears to be continuous instead of restarting for nested lists.</p>
';

  $phpWord = new PhpWord();
  $section = $phpWord->addSection();
  Html::addHtml($section, $html, false, false);
  $outputFile = 'word.2557.docx';
  $phpWord->save($outputFile);

That will produce the correct result. image

CISPDEVELOP commented 2 months ago

Hi, @oleibman, @marievyyy I have the same problem. Even if you correct the closing of the tag, the numbering continues to increase when you change to another ${var}

The strange thing is that if you put 2 numbered lists in 1 same variable, the numbering is respected, but when you change the variable and print another html list (with different numbering), the numbering is inherited from the first list that was printed. How can we reset this value since it should be treated as a completely different ${template_variable} element

Current behavior: imagen

Expected: imagen

I have tried many ways and none of them work like: adding a starts in 'ol' tags, adding page breaks in template, adding the element as a setValue and setComplexBlock via a Table() or Section() object, parsing the html content with another library using HTMLtoOpenXML\Parser() from rkorebrits/htmltoopenxml, and of course using the Shared\Html::addHtml(), and nothing works. If you have any way to solve it, I appreciate your help.

Thanks!