Forum


Replies: 1   Views: 344
Justify contents with "replacevariablebyhtml"
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  · 02-02-2024 - 17:23

Hello,

Please note that text-align is a paragraph style (https://www.phpdocx.com/htmlapi-documentation/html-standard/insert-paragraph-text-Word-document-with-HTML), not a span style (https://www.phpdocx.com/htmlapi-documentation/html-standard/insert-span-text-Word-document-with-HTML). You need to apply the text-align style to a paragraph tag and do a block type replacement:

$html = '<p style="text-align: justify;"><span style="font-family: Arial; font-size: 12px;">Text content</span></p>';
$docx->replaceVariableByHTML('TERMSHEET_27', 'block', $html);

A DOCX document doesn't allow applying a text-align style to run-of-text contents, only to paragraphs and similar contents.

(We have removed the stylesReplacementType option from the code because it's a HTML Extended feature only available in Premium licenses [https://www.phpdocx.com/documentation/introduction/html-extended-to-word-PHP], and you are using an Advanced license).

When you do an inline replacement, all paragraph styles are removed from the new content to be added. If you need to keep the text-align (and any other paragraph style) when using replaceVariableByHTML or replaceVariableByWordFragment, you need to do a block type replacement, that replaces the whole paragraph that contains the placeholder and add the new content. On https://www.phpdocx.com/api-documentation/templates/replace-variable-html-Word-document you can read about both replacement types when using replaceVariableByHTML.

Regards.