Moutinho
Moutinho

Reputation: 349

PHPWord/TemplateProcessor How to make paragraphs

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?

Upvotes: 0

Views: 2250

Answers (2)

Cevher Bilger
Cevher Bilger

Reputation: 21

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);

Upvotes: 2

Maria
Maria

Reputation: 161

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.

Upvotes: 0

Related Questions