Jonathan Weinraub
Jonathan Weinraub

Reputation: 387

PHPWord LineBreak in TextRun not working

I found advice from an older Stack Over Flow Question and I am having problems getting it to work. I added the methods where they described but the generated word file does not have the linebreaks (visually and by inspecting the docx file after unzipping it). There are no errors so the method seems to be doing something just not what I want. I am doing this since I need \n characters and I did get this to work fine using PHPRtfText but had issues with compatibility and it seems to work better as Word2007 files so I converted the code accordingly. Though, maybe I placed it in the wrong spot, but here is the modified function:

/lib/PHPWord/Writer/Word2007/Base.php:

protected function _writeTextRun(PHPWord_Shared_XMLWriter $objWriter = null,    PHPWord_Section_TextRun $textrun)
{
  $elements = $textrun->getElements();
  $styleParagraph = $textrun->getParagraphStyle();

  $SpIsObject = ($styleParagraph instanceof PHPWord_Style_Paragraph) ? true : false;

  $objWriter->startElement('w:p');

  if($SpIsObject)
  {
     $this->_writeParagraphStyle($objWriter, $styleParagraph);
  }
  elseif(!$SpIsObject && !is_null($styleParagraph))
  {
     $objWriter->startElement('w:pPr');
        $objWriter->startElement('w:pStyle');
           $objWriter->writeAttribute('w:val', $styleParagraph);
        $objWriter->endElement();
     $objWriter->endElement();
  }

  if(count($elements) > 0)
  {
     foreach($elements as $element)
     {
        if($element instanceof PHPWord_Section_Text)
        {
           $this->_writeText($objWriter, $element, true);
        }
        elseif($element instanceof PHPWord_Section_Link)
        {
           $this->_writeLink($objWriter, $element, true);
        }
     }
  }
  elseif($element instanceof PHPWord_Section_TextBreak)
  {
     $objWriter->writeElement('w:br');
  }

  $objWriter->endElement();
}

I am invoking it by:

test1.php:

$PHPWord = new PHPWord();;
$PHPWord->setDefaultFontName('Calibri');
$PHPWord->setDefaultFontSize(11);

$section = $PHPWord->createSection();
$textrun = $section->createTextRun();
$textrun->addText( "---------------");
$textrun->addTextBreak();
$textrun->addText( "$name", array('bold'=>true ) );

And lastly in /lib/PHPWord/Section/TextRun.php:

public function addTextBreak($count = 1)
{
    for($i=1; $i<=$count; $i++)
    {
        $this->_elementCollection[] = new PHPWord_Section_TextBreak();
    }
}

Is there anything else I can add to help? I really need this to work and cannot change the class again to something else.

I am heavily formatting the text, thus the textrun, but I need the \n after each line not a section as it needs to be the same paragraph, so having \n\n is not going to be acceptable for this endeavour.

Upvotes: 1

Views: 2800

Answers (1)

Jonathan Weinraub
Jonathan Weinraub

Reputation: 387

I definitely thought I tried this but perhaps I didn't save it on the webserver but moving the elseif into the higher up elseif block did the trick.

if(count($elements) > 0) 
  {
     foreach($elements as $element) 
     {
        if($element instanceof PHPWord_Section_Text) 
        {
           $this->_writeText($objWriter, $element, true);
            } 
        elseif($element instanceof PHPWord_Section_Link) 
        {
           $this->_writeLink($objWriter, $element, true);
            }
        elseif($element instanceof PHPWord_Section_TextBreak)
        {        
           $objWriter->writeElement('w:br');
        }              
     }
  } 
  $objWriter->endElement();
}

Upvotes: 0

Related Questions