Forum


Replies: 4   Views: 4297
Text align html to docx
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 andrewk_777  · 23-05-2018 - 14:57

i use <p align="center"> tag to align text

$html = '<p align="center">text</p>';

then i replace variable in the document using:

$docx->replaceVariableByHTML($section, 'inline', $html);

i get 0 effect for alignment.

should i use a different tag?

Posted by admin  · 23-05-2018 - 17:03

Hello,

The problem is that you are doing an inline replacement. Using this kind of replacement, block elements are removed and only the variable is replaced.

We recommend you to read the information available on:

https://www.phpdocx.com/documentation/introduction/html-to-word-PHP (Inserting HTML into Word templates section)

https://www.phpdocx.com/api-documentation/templates/replace-variable-html-Word-document (Parameters section)

MS Word allows setting the alignment to paragraphs, tables, images... You need to use a block type replacement to set a new alignment:

$docx->replaceVariableByHTML($section, 'block', $html);

Regards.

Posted by andrewk_777  · 23-05-2018 - 17:18

that did the trick, the only issue is that block does not presevse font family of the document. (calibri), how can i do that?

Posted by admin  · 24-05-2018 - 07:09

Hello,

UPDATE: phpdocx Premium licenses include the stylesReplacementType option to mix Word and HTML styles when using replaceVariableByHTML.

A block type replacement remove the container block tag and add a new one. The solution to keep the tag properties is using an inline replacement, but then you can't change container styles.

We recommend you to set the styles of the container (such as the alignment) in the template you are using and then use an inline replacement to keep the styles. Or add a CSS style to set the font family when doing a block replacement.

Changing styles on-the-fly is only available using DOCXCustomizer available in Premium licenses (https://www.phpdocx.com/documentation/introduction/docxcustomizer). Using this feature you can do inline replacements and change style tags if needed.

Regards.

Posted by andrewk_777  · 24-05-2018 - 15:13

ok i figured it out. so for those who are looking for a solution on how to edit fonts for styles for $docx->replaceVariableByHTML($section, 'block', $html);

 

PHPDocx\classes\HTML2WordML.inc line 1670:

$stringRPr .= '<w:rFonts w:ascii="' . $font . '" w:hAnsi="' . $font . '" w:eastAsia="' . $font . '" w:cs="' . $font . '" /> ';

i setting up the fonts. so if you are lf you a looking for a specific font style that you use in your template:

1. rename your docx file to .zip
2. open it up and open then open word folder then open document.xml
3. fint what fonts are currently used in the current word document by finding: <w:rFonts tag and replacing it inc file or doing if statements.

anyways just a general tip. worked for me