Forum


Replies: 1   Views: 998
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 royh  · 16-07-2021 - 14:53

Good day,

I am using replaceVariableByHTML to display text but can't seem to display the text in the HTML colors defined.

Here is the HTML and text:

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

I can't seem to figure out what replacements I need to add to see the TEXT in #00ff00.

Thanks.

Posted by admin  · 17-07-2021 - 08:07

Hello,

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>
</ol>';
$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>
</ol>';
$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.

Regards.