Forum


Replies: 3   Views: 176
Embedhtml sets both cell shading and text highlight

Posted by mschill  · 12-05-2025 - 07:59

Dear PHPDocx Team,

We are using embedHTML to export structured HTML tables to Word.
We apply style="background-color: #D9D9D9" directly to <td> elements in order to highlight specific table rows.

In the generated DOCX file, the cell appears correctly shaded.

However, it seems that an additional text highlight (w:highlight) is also applied.
As a result, even though the cell looks fine visually, users must also manually adjust or remove the text highlight when changing the shading color in Word – otherwise, the text retains the original background color from the highlight.

Our question:

    Is there a way to use embedHTML() so that only cell shading is applied, without any additional text highlighting?

    If not, would you consider adding an option such as "textHighlight" => false or "pureCellBackground" in a future release?

Thank you very much in advance for your feedback!

Best regards,
Anna

Posted by admin  · 12-05-2025 - 09:46

Hello,

What version of phpdocx are you using? phpdocx 16 can perfectly do the requested task.

Regards.

Posted by mschill  · 23-05-2025 - 08:54

Thank you for your reply. I have now upgraded to phpdocx 16 and just sent an email, as requested, including a screenshot and test details.

Looking forward to the sample script that disables text highlighting when using embedHTML().

Posted by admin  · 23-05-2025 - 09:25

Hello,

The easiest approach with phpdocx 16 to accomplish the requested task is to apply a transparent background to the cell contents. Please note that you need to wrap the text contents in tags (such as p or span ) to apply styles to the contents.

For example:

$html = '
<style>
    td {
        background-color: yellow;
    }
    td p, td span {
        background-color: transparent;
    }
</style>
<table border="1" style="border-collapse: collapse" width="500">
    <tbody>
        <tr>
            <td width="400"><p>1_1</p></td>
            <td width="400"><p>1_2</p></td>
        </tr>
        <tr>
            <td><span>2_1</span></td>
            <td><span>2_2</span></td>
        </tr>
    </tbody>
</table>';
$docx->embedHTML($html);

This same approach may work with some previous phpdocx versions.

Regards.