PHPOffice / PHPWord

A pure PHP library for reading and writing word processing documents
https://phpoffice.github.io/PHPWord/
Other
7.22k stars 2.69k forks source link

ErrorException in Html.php line 168: Trying to get property of non-object #1552

Open gitDharani opened 5 years ago

gitDharani commented 5 years ago

I am using laravel php office. i want to export word option from blade file.

Here is My Controller:

        $view = View::make('cp.survey.word', $data);

// or
$contents = $view->render();

$headers = array(
    "Content-type"=>"text/html",
    "Content-Disposition"=>"attachment;Filename=survey.doc"
);

$content = $contents;

  $phpWord = new \PhpOffice\PhpWord\PhpWord();

        $phpWord->setDefaultFontName('Times New Roman');
        $phpWord->setDefaultFontSize(12);
        $phpWord->getSettings()->setZoom(75);
        $section = $phpWord->addSection();
        $section->addText("asdasdasd");
        libxml_use_internal_errors(true);
$doc = new \DOMDocument();
$doc->loadHTML($content);
$content = $doc->saveHTML();
 \PhpOffice\PhpWord\Shared\Html::addHtml($section, $content,false,false);

         $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
        try {
            $objWriter->save(storage_path('helloWorld.docx'));
        } catch (Exception $e) {
        } 

        return response()->download(storage_path('helloWorld.docx')); 

Please help me to solve this error as soon as possible i got above error ErrorException in Html.php line 168: Trying to get property of non-object

troosan commented 5 years ago

Can you print the value of $content, does it contain a <body>tag?

vikashrajgupta commented 3 years ago

@troosan - I'm facing a similar kind of issue and also posted the question here https://github.com/PHPOffice/PHPWord/issues/2134 Can you please help me out? my string doesn't contain the <body> tag.

I'm getting the below errors:

2021-08-21 07:35:18 Notice: Notice (8): Trying to get property 'nodeName' of non-object in [/var/www/html/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php, line 168]

2021-08-21 07:35:20 Notice: Notice (8): Trying to get property 'childNodes' of non-object in [/var/www/html/vendor/phpoffice/phpword/src/PhpWord/Shared/Html.php, line 208]