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

new line in template #838

Open
fishwolf opened this issue Jul 18, 2016 · 7 comments
Open

new line in template #838

fishwolf opened this issue Jul 18, 2016 · 7 comments

Comments

@fishwolf
Copy link

fishwolf commented Jul 18, 2016

I'm using template processo for create a word document.
setValue method work fine, but i need set 3 strings in 3 lines.

String1
String2
String3

I have tried to add "\n"

String1\nString2\nString3

but this work fine ONLY with libreoffice, with microsoft office doesn't work fine.

I have tried to add '< w : b r / >' (without space) but dosen't work on libreoffice

Any suggestions?


Want to back this issue? Post a bounty on it! We accept bounties via Bountysource.

@antho2930
Copy link

antho2930 commented Aug 30, 2017

I just tried with "< w : b r / >\n" (without space) and it seems to work both in MS Office and LibreOffice.

@SalvatorePollaci
Copy link

@antho2930 Your solution is working for me but new lines are prefixed with an intial whitespace.

@FBnil
Copy link

FBnil commented Oct 4, 2017

@antho2930 @SalvatorePollaci You need to replace \n with </w:t><w:br/><w:t> the replacement string is without the \n. See #268

@weiddolo
Copy link

weiddolo commented Jul 17, 2020

@antho2930 @SalvatorePollaci You need to replace \n with </w:t><w:br/><w:t> the replacement string is without the \n. See #268

How i can add <hr> horizontal line like <br> (</w:t><w:br/><w:t>)?

@Francesco-Manicardi
Copy link

Francesco-Manicardi commented Jan 10, 2021

Here's the true answer to "How to add newline while template processing"

You can't just replace \n with </w:t><w:br/><w:t> because that will BREAK PARAGRAPHS.
This means that tab stops that have been set on the placeholder will not carry over to the next line!

The real solution is to always use a complex type instead of using strings.

So in my function instead of returning a string I do this:

            $res = "some text\n with\n newlines\tand\ttabs
            $textlines = explode("\n", $res);
            $textrun = new PhpOffice\PhpWord\Element\TextRun();
            $textrun->addText(array_shift($textlines));
            foreach ($textlines as $line) {
                $textrun->addTextBreak();
                $textrun->addText($line);
            }
            return $textrun;

and in the template processor driver I use:

$templateProcessor->setComplexValue($key, $textrun);

this will result in newlines and tab stops and other paragraph related things are preserved in the next lines!

This allows you to create basic tables with tab stops and not worry about them breaking after the new lines.

@Gadeoli
Copy link

Gadeoli commented Jan 21, 2021

Here's the true answer to "How to add newline while template processing"

You can't just replace \n with </w:t><w:br/><w:t> because that will BREAK PARAGRAPHS.
This means that tab stops that have been set on the placeholder will not carry over to the next line!

The real solution is to always use a complex type instead of using strings.

So in my function instead of returning a string I do this:

            $res = "some text\n with\n newlines\tand\ttabs
            $textlines = explode("\n", $res);
            $textrun = new PhpOffice\PhpWord\Element\TextRun();
            $textrun->addText(array_shift($textlines));
            foreach ($textlines as $line) {
                $textrun->addTextBreak();
                $textrun->addText($line);
            }
            return $textrun;

and in the template processor driver I use:

$templateProcessor->setComplexValue($key, $textrun);

this will result in newlines and tab stops and other paragraph related things are preserved in the next lines!

This allows you to create basic tables with tab stops and not worry about them breaking after the new lines.

Works, but for me cause problems with justify content (extra spaces between words). Maybe is because is using soft lines instead of paragraph breaks like said here.

Fallow ooxml i try to fix it with the using the bellow code:

//My custom function to replace values
//I pre config a default style and paragraph style...
public function setTProcValue(&$template, $key, $value, $nonInfomedCheck = false, $fixedNonValue = '', $options = [], $pOptions = []){
    $aux = $this->fixBreakSpaces($value);

    if($aux && strpos($aux, $this->newBr) !== false){
        $style = array_merge($this->style, $options);
        $pStyle = array_merge($this->pStyle, $pOptions);

        $lines = explode($this->newBr, $aux);
        $txtR = new TextRun();

        for ($i=0; $i < count($lines); $i++) { 
            if($i > 0){
                // $txtR->addTextBreak(1, $style, $pStyle); //justify extra space problem
                // $txtR->addText('</w:t><w:br/><w:t xml:space="preserve">'); //justify extra space problem
                $txtR->addText('</w:t></w:r></w:p><w:p><w:pPr><w:jc w:val="both"/></w:pPr><w:r><w:t>'); 
            }
            $txtR->addText(htmlspecialchars($lines[$i], ENT_QUOTES, 'UTF-8'), $style, $pStyle);
        }
        
        $template->setComplexValue($key, $txtR);
    }else if($nonInfomedCheck && ($value === '' || $value === null)){
        if($fixedNonValue){
            $value = $fixedNonValue;
        }else{
            //$value = \Lang::get('validation.error.default.notInformed', [], $this->locale);
            $value = '-';
        }

        $template->setValue($key, $value);
    }else{
        $template->setValue($key, htmlspecialchars($value, ENT_QUOTES, 'UTF-8'));
    }
}

//Replace break lines from storage data
public function fixBreakSpaces($value){
    $oldBrs = ['<br />', '</br>', '<br>', '\r\n', '\n'];

    $value = json_encode(['txt'=>$value]);
    $value = str_replace($oldBrs, $this->newBr, $value);

    $value = json_decode($value);

    return $value->txt;
}

This works for me with multiple spaces replaces and allow to keep using justify and custom alingments, styles if i need.

@viplike
Copy link

viplike commented May 23, 2023

I have tried ALL the posted decisions on github, stackoverflow, different sites, I was googling for 3 days and nothing worked. So I went to the docx document, renamed its extension to the zip, and checked the document xml schema.

So there is a bit of code, that can be used for inserting a new line without text break bug:

<?php
$textRun = new TextRun();
$textRun>addText('A long text here');

// new line
$textRun->addText('</w:t></w:r></w:p>
    <w:p>
      <w:pPr>
      <w:contextualSpacing/>
        <w:jc w:val="both"/>
        <w:rPr>
          <w:rFonts w:ascii="Times New Roman" w:hAnsi="Times New Roman"/>
        </w:rPr>
      </w:pPr>
      <w:r>
      <w:t>');

// a second row
$textRun>addText('Another long text here');

// replace a variable
$doc->setComplexValue('Variable', $textRun);

In my case I'm using Times New Roman, and justify text alignment. You can set your own options for the paragraph, but you should specify them in ooxml format using tags. Hope this helps somebody

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

No branches or pull requests

8 participants