PHPWord icon indicating copy to clipboard operation
PHPWord copied to clipboard

PHPWord randomly deletes whitespaces

Open mvts opened this issue 8 years ago • 18 comments

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.

mvts avatar Aug 06 '15 10:08 mvts

Can you give an example document? I'm not able to reproduce your problem.

maximeplante avatar Aug 09 '15 17:08 maximeplante

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.

iflow avatar Nov 11 '15 08:11 iflow

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

mapyourproperty avatar Nov 23 '15 20:11 mapyourproperty

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?

mhollander avatar Jan 07 '16 03:01 mhollander

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

mapyourproperty avatar Jan 07 '16 03:01 mapyourproperty

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.

mhollander avatar Jan 08 '16 02:01 mhollander

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.

Ravaelles avatar Jan 25 '18 12:01 Ravaelles

I have same issue, When the word is generated some space are missing, any idea?

theenescresta avatar Jan 05 '19 11:01 theenescresta

Even with latest version (0.16)?

troosan avatar Jan 06 '19 18:01 troosan

i am using 0.14

theenescresta avatar Jan 07 '19 01:01 theenescresta

Could you please try with the latest version.

troosan avatar Jan 07 '19 20:01 troosan

it's also happening with the version 0.17.0

florianziltener avatar Apr 08 '20 06:04 florianziltener

Executing this before the save solved it for me: $this->tempDocumentMainPart = preg_replace('/(<w:t*)>/', '$1 xml:space="preserve">', $this->tempDocumentMainPart);

florianziltener avatar Apr 14 '20 20:04 florianziltener

@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!

modestguy avatar May 15 '20 09:05 modestguy

I can confirm the error too. modestguy's fix is working. Please pull.

nQk2 avatar Jul 15 '21 13:07 nQk2

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".

melino avatar Jan 25 '22 17:01 melino

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.

ngekoding avatar Jun 24 '22 02:06 ngekoding

@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

aysegulYalcinkaya avatar Aug 19 '22 00:08 aysegulYalcinkaya