Forum


Replies: 1   Views: 372
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 Dale.Cooper  · 02-02-2024 - 16:34

Hallo!

I am using a DOCX Template to fill out a lot of placeholder-variables with HTML chucks coming from another system.

All formats I need (bold, italic, underline) get handed over correctly. However, text-align: justify is not recognized.

I am using this to insert the code:

$docx->replaceVariableByHTML('TERMSHEET_27', 'inline', $ph_termsheet_27, array('stylesReplacementType' => 'mixPlaceholderStyles'));

Using block instead of inline makes no difference.

The HTML tag used looks like this:
<span style="text-align: justify; font-family: Arial; font-size: 12px">

It's also ok for me to hard-code the justify within PHP while inserting if this is also a solution.

Thanks for any help!

Marc

 

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.