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

Call to undefined method PhpOffice\PhpWord\Element\TextBreak::getText() #1573

Open
1 of 3 tasks
maksatakashayev210998 opened this issue Feb 9, 2019 · 5 comments
Open
1 of 3 tasks

Comments

@maksatakashayev210998
Copy link

maksatakashayev210998 commented Feb 9, 2019

This is:

Expected Behavior

Please describe the behavior you are expecting.

Current Behavior

What is the current behavior?

Failure Information

Please help provide information about the failure.

How to Reproduce

Please provide a code sample that reproduces the issue.

<?php
require __DIR__ . '/vendor/autoload.php';

$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$section->...

Context

  • PHP version:
  • PHPWord version: 0.14
@maksatakashayev210998 maksatakashayev210998 changed the title what the fuck is this???? what the fuck is this???? Call to undefined method PhpOffice\PhpWord\Element\TextBreak::getText() Feb 9, 2019
@maksatakashayev210998
Copy link
Author

maksatakashayev210998 commented Feb 9, 2019

I am trying to read docx file, code:

$df = $query1->filename;
if($query1->raw=="docx"){
    $source = '../web/doc/'.$df.'';
    $objReader = IOFactory::createReader('Word2007');
    $phpWord = $objReader->load($source);
    $body = '';
    
    foreach ($phpWord->getSections() as $section) {
      $arrays = $section->getElements();
      $body .='<p>';
      foreach ($arrays as $e) {
        if(get_class($e) === 'PhpOffice\PhpWord\Element\TextBreak'){
          $body .='<br>';
        }else
        if(get_class($e) === 'PhpOffice\PhpWord\Element\TextRun') {
          foreach($e->getElements() as $text) {
            //$font = $text->getFontStyle();
            //$size = $font->getSize();
            //$bold = $font->isBold() ? 'font-weight:700;' :'';
            //$color = $font->getColor();
            //if($font->isItalic()){
              //$ital = 'italic';
            //} else {$ital = '';};
            //$fontFamily = $font->getName();
            $body = $body.'<span style="float:left;font-size:14px;">'.$text->getText().'</span>';
          }
        }  
        if(get_class($e) === 'PhpOffice\PhpWord\Element\Text'){
            $font = $e->getFontStyle();
            $size = $font->getSize();
            $bold = $font->isBold() ? 'font-weight:700;' :'';
            $color = $font->getColor();
            if($font->isItalic()){
              $ital = 'italic';
            } else {$ital = '';};
            $fontFamily = $font->getName();
            $body .='<span style="display:inline;text-align:left;font-style:'.$ital.';font-size:14px;font-family:'.$fontFamily .';'.$bold.'; color:#'.$color.';'.$line.'">';
            $body .=$e->getText().'</span>';
        }
        if(get_class($e) === 'PhpOffice\PhpWord\Element\Image'){
          $body .= '<div style="width:200px;height:150px; background:red;"></div>';
        } 
        if(get_class($e) === 'PhpOffice\PhpWord\Element\Table') {
          $body .= '<table border="2px">';
               
          $rows = $e->getRows();
               
          foreach($rows as $row) {
            $body .= '<tr>';
               
            $cells = $row->getCells();
            foreach($cells as $cell) {
               $body .= '<td style="width:'.$cell->getWidth().'">';
               $celements = $cell->getElements();
                foreach($celements as $celem) {
                  if(get_class($celem) === 'PhpOffice\PhpWord\Element\Text') {
                    $body .= $celem->getText();
                  }
             
                else if(get_class($celem) === 'PhpOffice\PhpWord\Element\TextRun') {
                foreach($celem->getElements() as $text) {
                  $body .= $text->getText();
                }  
                }
              } 
              $body .= '</td>';
              }
                $body .= '</tr>';
              }
                $body .= '</table>';
              }

              if (get_class($e)==='PhpOffice\PhpWord\Element\ListItem'){
               $list = new \PhpOffice\PhpWord\Style\ListItem();
               $listType .= $list->getListType();
               if($listType === 7) {
                   $lts = '<ol>';
                   $lte = '</ol>';
               }
               else if($listType === 3) {
                   $lts = '<ul>';
                   $lte = '</ul>';
               }
                           $body .='<ul style="font-size:14px; color:black; font-family:Times-New-Roman;>';

                                  $ee = 'PhpOffice\PhpWord\Element\ListItem';

                                   $obj = $e->getTextObject();


                                       $body .='<li style="color:'.$color.';">';

                                      if(get_class($obj)==='PhpOffice\PhpWord\Element\Text'){


                                           $body .=$obj->getText();

                                       }


                                   $body .='</li>';

                            $body .='</ul>';
           }
              // else {
              //   $body .= $e->getText();
              // }
      }
      $body .='</p>';
      break;
    }   
}

@maksatakashayev210998
Copy link
Author

But I get an error, Call to undefined method PhpOffice\PhpWord\Element\TextBreak::getText()

@troosan troosan changed the title what the fuck is this???? Call to undefined method PhpOffice\PhpWord\Element\TextBreak::getText() Call to undefined method PhpOffice\PhpWord\Element\TextBreak::getText() Feb 11, 2019
@troosan
Copy link
Contributor

troosan commented Feb 11, 2019

Do you have a complete stacktrace? I guess it fails when loading the document? $phpWord = $objReader->load($source);
If so, do you have a sample document you can share? Or just the source XML?

@vika-bhardwaj
Copy link

Hi Troosan,

I am also getting similar error with function getParagraphStyle()
Following is the issue:
Fatal error: Uncaught Error: Call to undefined method PhpOffice\PhpWord\Element\PageBreak::getParagraphStyle()

I am using following code:
function docx2html($source)
{
$phpWord =\PhpOffice\PhpWord\IOFactory::load($source);
$html = '';
foreach ($phpWord->getSections() as $section) {
foreach ($section->getElements() as $ele1) {
$paragraphStyle = $ele1->getParagraphStyle();
if ($paragraphStyle) {
$html .= '

';
} else {
$html .= '

';
}
if ($ele1 instanceof\PhpOffice\PhpWord\Element\TextRun) {
foreach ($ele1->getElements() as $ele2) {
if ($ele2 instanceof\PhpOffice\PhpWord\Element\Text) {
$style = $ele2->getFontStyle();
$fontFamily = mb_convert_encoding($style->getName(), 'GBK', 'UTF-8');
$fontSize = $style->getSize();
$isBold = $style->isBold();
$styleString = '';
$fontFamily && $styleString .= "font-family:{$fontFamily};";
$fontSize && $styleString .= "font-size:{$fontSize}px;";
$isBold && $styleString .= "font-weight:bold;";
$html .= sprintf('%s',
$styleString,
mb_convert_encoding($ele2->getText(), 'GBK', 'UTF-8')
);
} elseif ($ele2 instanceof\PhpOffice\PhpWord\Element\Image) {
$imageSrc = 'images/' . md5($ele2->getSource()) . '.' . $ele2->getImageExtension();
$imageData = $ele2->getImageStringData(true);
// $imageData = 'data:' . $ele2->getImageType() . ';base64,' . $imageData;
file_put_contents($imageSrc, base64_decode($imageData));
$html .= '';
}
}
}
$html .= '

';
}
}

return mb_convert_encoding($html, 'UTF-8', 'GBK');

}

Code for calling above function:
require_once 'PHPWord/vendor/autoload.php';
$dir = str_replace('\', '/', DIR) . '/';
$file_name = 'TIMT-TEST-FIELDS3.docx';
$source = $dir . $file_name;
$content = $this->docx2html($source);

I have attached source file, please check and let me know if there is a solution.
TIMT-TEST-FIELDS3.docx

@thomasb88
Copy link

As there can be TextBreak inside TextRun, doing a for loop on TextRun and then applying getText from childs lead to try to call getText for TextBreak, and so lead to the error.

For the second one, PageBreak is supposed to be a child of Section object. So that $ele1 can be a PageBreak and PageBreak is not having a method called getParagraphStyle

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

4 participants