php – PHPWord/TemplateProcessor How to make paragraphs-ThrowExceptions

Exception or error:

I have text in a with paragraphs, spaces and justied text like this coming from the database.

Hello.

My name is John.

Thank you

But when I use PHP Word with TemplateProcessor to move to a Word document it generates everything without paragraphs.

One solution I found for this was to do this:

$text=preg_replace('/\v+|\\\r\\\n/','<w:p/>',$TextWhitoutParagraphs);

He actually writes with paragraphs but only the first paragraph is justified.

How do I do this correctly with paragraphs and all text justified?

How to solve:

You can use cloneBlock. I used it with justified text and it worked perfectly. In your template use:

${paragraph}
${text}
${/paragraph}

And then explode your string by”\n”:

$textData = explode("\n", $text);
$replacements = [];
foreach($textData as $text) {
    $replacements[] = ['text' => $text];
}
$templateProcessor->cloneBlock('paragraph', count($replacements), true, false, $replacements);

Answer´╝Ü

The TemplateProcessor can only be used with single line strings (see docs: “Only single-line values can be replaced.” http://phpword.readthedocs.io/en/latest/templates-processing.html)

What you could try is replacing your new lines with ” (closing the opened paragraph and starting a new one), but that would just be my guess right now. It always helps to check the resulting Word-XML for syntax-errors.

Leave a Reply

Your email address will not be published. Required fields are marked *