PHPWord
PHPWord copied to clipboard
PHPWord randomly deletes whitespaces
PHPWord seems to randomly delete whitespaces from my docx Documents. In the templates, the spaces are there. But when I render them in PHPWord, the new Document has some whitespaces missing. Anyone else have this problem?
Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.
Can you give an example document? I'm not able to reproduce your problem.
Same problem here. If you edit the PHPWord template with LibreOffice 5, the whitespaces are missing between the PHPWord template variables after generating the docx file.
We had the problem where whitespaces are added, especially after a title or end or beginning of a bullet series. Difficult to control when to have or not have whitespaces
I have the same problem where Templates lose whitespaces wherever I have substitutions. Sometimes the whitespace is lost between a substitution and the non-substituted text next to it.
For example, if I have: Insert some ${VAR} here
and I do a setValue("VAR", "text"), I will get:
insert sometexthere
Frustrating! Is there a fix for this?
We have the same problem. Sometimes you can fix it sometimes you can't. It especially is a problem with whitespace or spacing problem after bullets or numerical. Not aware of any fix but you can check the X.1 version
I found that I was able to fix any whitespace issues by retyping the area around where the whitespace was deleted. I think what is happening is that the document.xml file has created lots of structure around your text that doesn't need to be there. PHPWord removes that structure and accidentally deletes the spaces. If you rewrite the text, the extra structure goes away.
I've also noticed, than when you search & replace a variable, if the same variable is present more than once, it causes problems. For example:
{email} Bla bla {email address}
is better than just {email} twice and in my case preserves whitespace.
I have same issue, When the word is generated some space are missing, any idea?
Even with latest version (0.16)?
i am using 0.14
Could you please try with the latest version.
it's also happening with the version 0.17.0
Executing this before the save solved it for me: $this->tempDocumentMainPart = preg_replace('/(<w:t*)>/', '$1 xml:space="preserve">', $this->tempDocumentMainPart);
@florianziltener Thank you very much, bro! I extended the class TemplateProcessor and override save method like this: `<?php
namespace common\components\phpoffice;
use PhpOffice\PhpWord\TemplateProcessor;
class ExtendedTemplateProcessor extends TemplateProcessor { public function save() { $this->tempDocumentMainPart = preg_replace('/(<w:t*)>/', '$1 xml:space="preserve">', $this->tempDocumentMainPart); foreach ($this->tempDocumentHeaders as $index => $xml) { $this->savePartWithRels($this->getHeaderName($index), $xml); }
$this->savePartWithRels($this->getMainPartName(), $this->tempDocumentMainPart);
$this->savePartWithRels($this->getSettingsPartName(), $this->tempDocumentSettingsPart);
foreach ($this->tempDocumentFooters as $index => $xml) {
$this->savePartWithRels($this->getFooterName($index), $xml);
}
$this->zipClass->addFromString($this->getDocumentContentTypesName(), $this->tempDocumentContentTypes);
if (false === $this->zipClass->close()) {
throw new \Exception('Could not close zip file.');
}
return $this->tempDocumentFilename;
}
}`
And it works fine! Thanks again!
I can confirm the error too. modestguy's fix is working. Please pull.
I still have this issue with 0.18.2 but realized I just have to delete the trailing space in the template. Like mhollander, I think the underlying xml data sometimes is incompatible with phpWord.
So if you have "${variableValue} constantText" and the result is "123constantText", just delete the space and write it again. In my case this 'repaired' the xml and the result became the desired "123 constantText".
I still have this issue with 0.18.2 but realized I just have to delete the trailing space in the template. Like mhollander, I think the underlying xml data sometimes is incompatible with phpWord.
So if you have "${variableValue} constantText" and the result is "123constantText", just delete the space and write it again. In my case this 'repaired' the xml and the result became the desired "123 constantText".
Thank you, I get this issue today and fixed by as you tell.
@florianziltener Thank you very much, bro! I extended the class TemplateProcessor and override save method like this: `<?php
namespace common\components\phpoffice;
use PhpOffice\PhpWord\TemplateProcessor;
class ExtendedTemplateProcessor extends TemplateProcessor { public function save() { $this->tempDocumentMainPart = preg_replace('/(<w:t*)>/', '$1 xml:space="preserve">', $this->tempDocumentMainPart); foreach ($this->tempDocumentHeaders as $index => $xml) { $this->savePartWithRels($this->getHeaderName($index), $xml); }
$this->savePartWithRels($this->getMainPartName(), $this->tempDocumentMainPart); $this->savePartWithRels($this->getSettingsPartName(), $this->tempDocumentSettingsPart); foreach ($this->tempDocumentFooters as $index => $xml) { $this->savePartWithRels($this->getFooterName($index), $xml); } $this->zipClass->addFromString($this->getDocumentContentTypesName(), $this->tempDocumentContentTypes); if (false === $this->zipClass->close()) { throw new \Exception('Could not close zip file.'); } return $this->tempDocumentFilename; }
}`
And it works fine! Thanks again!
Thank you so much. Works perfect