Replies: 1   Views: 1068
Html font colours
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 admin  · 17-07-2021 - 08:07


You can apply a color as any other supported CSS style when doing a replacement:

$docx = new CreateDocxFromTemplate('template.docx');
$html = '<ol>
<li><span style="color: #00ff00;">TEXT</span></li>
$docx->replaceVariableByHTML('VAR', 'block', $html);

If you want to use HTML Extended (available only in Premium licenses) with the replaceVariableByHTML method to mix existing styles in the placeholder and the styles added in the HTML then you need to set the correct styles you want to ignore from the HTML. If you want to add the color from the HTML you can't ignore the w:color tag. For example, the following code:

$html = '<ol>
<li><span style="color: #00ff00;">TEXT</span></li>
$stylesReplacementTypeIgnore = array('w:sz', 'w:szCs', 'w:rFonts');
$docx->replaceVariableByHTML('VAR', 'block', $html, array('stylesReplacementType' => 'mixPlaceholderStyles', 'stylesReplacementTypeIgnore' => $stylesReplacementTypeIgnore, 'useHTMLExtended' => true));

mixes placeholder and HTML styles ignoring the following styles from the HTML: w:sz and w:szCS (font size) and w:rFonts (font family). Your code is adding w:color to the ignored styles:

$stylesReplacementTypeIgnore = array('w:sz', 'w:szCs', 'w:rFonts', 'w:color');

so the color from the HTML is being ignored. You should never ignore the w:rPr tag because it's not a style but a wrapping tag.

Also note that when using the mixPlaceholderStyles option, styles set in a placeholder are not overriden by the imported HTML. For example, if you have set a color style in the placeholder of a template, enabling mixPlaceholderStyles won't add a new color from the HTML. You can ignore styles from the HTML you don't want to be applied but the existing placeholder styles won't be overridden by the same styles added from HTML.