PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents

Home Page:https://phpoffice.github.io/PHPWord/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exported Word Document Content Issue with List (numbering)

marievyyy opened this issue · comments

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

  • PHP Version: 8
  • PHPWord Version: 1.2.0 (latest)

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