Search code examples
docxopentbstinybutstrong

In OpenTBS: How to use "new paragraph" carriage returns instead of "new line" in MS Word Templates


I have a PHP variable holding plain text that i need to insert into a MS Word Template using OpenTBS.

My Word placeholder is:

[onshow.introduction]

The problem is I use justification formatting, so I need actual "new paragraph" ie "carriage returns" (Enter) in Word. Instead all my newlines are translated into "new line" (shift-Enter), messing up my formatting.

I tried inserting
, \r, \n, \n\r, \r\n, PHP_EOL - all of them just create newline characters instead of new paragraph characters in MS Word.

Is there any way to get "new paragraph" characters in Word from OpenTBS?


Solution

  • In Ms Word, a new paragraph is a complicated XML entity. Something like:

    <w:p><w:r><w:t>My paragraph</w:t></w:r></w:p>
    

    and it can have extra attributes and extra sub-entities that can appear at several places.

    So it would be very touchy and hazardous to replace your line-breaks with </w:t></w:r><w:r><w:t>.

    The best and robust solution is to explode your text in several parts, and merge it on a block bounded an Ms Word paragraph.

    Example :

    PHP:

    // uniformize line breaks
    $introduction = str_replace("\n", "\r", $introduction);
    $introduction = str_replace("\r\r", "\r", $introduction);
    
    // explode in several parts
    $introduction = explode("\r", $introduction);
    
    // merge on a block
    $TBS->MergeBlock('intro', $introduction);
    

    DOCX:

    [intro.val;block=tbs:p]