Monday, March 25, 2019

How to Convert HTML to DOCX Using PHP in laravel | Laravel.

Convert HTML To PDF

first of all you need to install package phpword

composer require phpoffice/phpword
Now:
<?php
/* [START PHPWORD] */
require "vendor/autoload.php";
$pw = new \PhpOffice\PhpWord\PhpWord();
/* [THE HTML] */
$section = $pw->addSection();
$html = "<h1>HELLO WORLD!</h1>";
$html .= "<p>This is a paragraph of random text</p>";
$html .= "<table><tr><td>A table</td><td>Cell</td></tr></table>";
\PhpOffice\PhpWord\Shared\Html::addHtml($section, $html, false, false);
/* [SAVE FILE ON THE SERVER] */
// $pw->save("html-to-doc.docx", "Word2007");
/* [OR FORCE DOWNLOAD] */
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment;filename="convert.docx"');
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($pw, 'Word2007');
$objWriter->save('php://output');
?>

1 comment: