0

How do I convert this unclean html tag into word document? I am totally lost here, I don't know what I am doing, my goal is just to covert this html into word document, doesn't need to be fancy or accurate, I just want it to display the layout and its contents, styling can be ignored as well. My code generates a doc file but cannot be opened due to problem found in the contents.

    $html = "<h3>Restaurant Brands</h3>
            <ul class='prodoplist'>
                <li>BJ's Restaurant &amp; Brewery</li>
                <li>BJ's Restaurant &amp; Brewhouse</li>
                <li>BJ's Pizza &amp; Grill</li>
                <li>BJ's Grill</li>
            </ul>
            <p><strong>Selected Menu Items</strong>
                <br>Shareable Appetizers
                <br>BJ's Snacks &amp; Small Bites
                <br>Lunch Specials
                <br>Sandwiches and Tacos
                <br>Handcrafted Burgers
                <br>Housemade Soups &amp; Salads
                <br>Starter Salads
                <br>Garden Fresh Specialty Salads
                <br>Beverages
                <br>Handcrafted Beers
                <br>Cocktails and Wine
                <br>Pizza
                <br>Pasta favorites
            </p>";


 $phpWord = new \PhpOffice\PhpWord\PhpWord();
 $phpWord->setDefaultFontSize(10);
 $phpWord->setDefaultFontName('Calibri');
 $phpWord->setDefaultParagraphStyle(
 array(
        'spaceAfter' => \PhpOffice\PhpWord\Shared\Converter::pointToTwip(0))
 );
 $metaDataSection = $phpWord->addSection();
 $table = $metaDataSection->addTable('Test');

 $breaks = array("<br />","<br>","<br/>");  
 $text = str_replace($breaks, "<br/>", $html); 
 $text = iconv('UTF-8', 'ASCII//IGNORE',$text);
 $text = "<html>". $text . "</html>";
 $table->addRow();
 $cell = $table->addCell(10000); \PhpOffice\PhpWord\Shared\Html::addHtml($cell, $text); 
 $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
apelidoko
  • 782
  • 1
  • 7
  • 23
  • @Cid my definition of unclean HTML is
    or

    might not have closing tag or some other HTML Tags might not have proper closing tag, it also doesn't have body, head and title tag

    – apelidoko Dec 15 '20 at 11:57
  • @Cid I have updated my question, my question will be how to convert the html I have to docx, I am encountering an error upon opening the generated doc file, it says the content is not supported – apelidoko Dec 15 '20 at 12:11

0 Answers0