Replies: 1   Views: 2317
Embedhtml - space in beginning and end
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 Brck  · 22-05-2018 - 13:11


In certain situations, I have text or html which begins and/or ends with a space. For instance:

$text = " and a previous sentence continues ";

$html " and a <i>previous</i> sentence continues ";

In using $docx->addText($text) the resulting docx include all spaces in the $text variable. Excellent.

In using $docx->embedHTML($html) then the first and last spaces (i.e. the " " before "and", the " " after "continues") are stripped. 

I have tried to replace any first or last space with &ensp; but that does not seem to correspond to ordinary spaces in the resulting docx. The &ensp; is larger than and ordinary space and does not show as a "dot" when i display formatting marks in Word/libreoffice.


Any idea?

Best regards,



Posted by admin  · 23-05-2018 - 06:45


You can use pre tags to keep extra spaces, but please note that you may need to set new styles to avoid default ones:

$docx->embedHTML('<p style="font-size: 16px; font-family: Calibri;">Sentence begins</p>');
$docx->embedHTML('<pre style="font-size: 16px; font-family: Calibri;"> and a previous   sentence continues </pre>');

Other approach would be using a WordFragment and adding spaces with addText:

$html = new WordFragment($docx);
$html->embedHTML('<p>and a previous sentence continues</p>');

$text = array();
$text[] = array('text' => ' ');
$text[] = $html;
$text[] = array('text' => ' ');