Forum


Replies: 3   Views: 84
Space characters removed in createdocxfromtemplate process...

Posted by miso  · 04-12-2024 - 15:28

Hmm... this is a challenge now, as we don't know to "fix" this.

Here's our code:

     $docx = new CreateDocxFromTemplate($input_file);
        $docx->setTemplateSymbol('%%', '%%');
        $first = 'phpdocx';
        $docx->replaceVariableByText($replacements);
        $docx->createDocx($output_file);

And in the $input_file there's a section of text that's separated and spaced out by blank spaces:

https://img001.prntscr.com/file/img001/EuYShWv7Q-KK2SOZqIg9Sg.jpeg

But in the $output_file that section ends up looking like this, with all the space characters removed:

https://img001.prntscr.com/file/img001/Sh3mYRNmTsGzeAEkoO64VA.jpeg

What are we missing?

Is there some sort of a parameter we can pass to that process/function to ignore space characters? Because we have 100+ of these templates that otherwise we'd have to go in and fix manually, set up this section of text in a table or something, I assume, that would convert better with phpdocx?

Suggestions?

Posted by admin  · 04-12-2024 - 16:49

Hello,

We recommend you read the documentation available at https://www.phpdocx.com/documentation/cookbook/good-practices-with-templates (Parse mode option section).

As explained on this page, if your DOCX contains the template placeholder symbols as regular contents, we recommend enabling the parseMode option:

$docx = new CreateDocxFromTemplate('template.docx', ['parseMode' => true]);

or use ${ } to wrap placeholders.

Please also send your DOCX to contact[at]phpdocx.com and we'll do some tests.

Regards.

Posted by miso  · 04-12-2024 - 17:16

Wow, that worked! 

I read that document but didn't really understand what you meant by that parseMode parameter... and quite honestly, at this point, I don't need to know, as long as it fixes this issue.

Thank you!

Posted by admin  · 04-12-2024 - 17:23

Hello,

Enabling the parseMode option is recommended when the symbols to wrap placeholders are not ${ } or use multiple characters (symbols) that are included in the template as regular content.
MS Word can break words in several tags, so phpdocx needs to repair them to handle placeholder replacements correctly.

If you don't enable the parseMode option, the whole XML content is parsed at once to clean placeholders. The parseMode option parses and analyzes paragraphs individually to get the placeholders.

Regards.