Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPWord randomly deletes whitespaces #590

Open
mvts opened this issue Aug 6, 2015 · 18 comments · May be fixed by #1971
Open

PHPWord randomly deletes whitespaces #590

mvts opened this issue Aug 6, 2015 · 18 comments · May be fixed by #1971

Comments

@mvts
Copy link

mvts commented Aug 6, 2015

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.

@maximeplante
Copy link

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

@iflow
Copy link

iflow commented Nov 11, 2015

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.

@mapyourproperty
Copy link

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

@mhollander
Copy link

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?

@mapyourproperty
Copy link

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

@mhollander
Copy link

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.

@Ravaelles
Copy link

Ravaelles commented Jan 25, 2018

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.

@theenescresta
Copy link

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

@troosan
Copy link
Contributor

troosan commented Jan 6, 2019

Even with latest version (0.16)?

@theenescresta
Copy link

i am using 0.14

@troosan
Copy link
Contributor

troosan commented Jan 7, 2019

Could you please try with the latest version.

@florianziltener
Copy link

it's also happening with the version 0.17.0

@florianziltener
Copy link

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

@modestguy
Copy link

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

@chapa chapa linked a pull request Nov 20, 2020 that will close this issue
3 tasks
@nQk2
Copy link

nQk2 commented Jul 15, 2021

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

@melino
Copy link

melino commented Jan 25, 2022

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

@ngekoding
Copy link

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.

@aysegulYalcinkaya
Copy link

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.