Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 26, 2023 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/12356057704ee7d3eaaf397a307cc58e to your computer and use it in GitHub Desktop.
Save code-boxx/12356057704ee7d3eaaf397a307cc58e to your computer and use it in GitHub Desktop.
PHP Convert HTML To DOCX

PHP CONVERT HTML TO DOCX

https://code-boxx.com/convert-html-to-docx-using-php/

NOTES

  1. A copy of PHPWord is not included, please download your own – composer require phpoffice/phpword.
  2. Run 2-convert.php and 3-more.php for the conversion demo.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1) Install Composer - https://getcomposer.org/download/
2) Run command prompt and navigate to your project folder.
3) Run "composer require phpoffice/phpword" in your project folder.
Alternatively - Manually download PHPWord from https://github.com/PHPOffice/PHPWord
<?php
// (A) LOAD PHPWORD
require "vendor/autoload.php";
$pw = new \PhpOffice\PhpWord\PhpWord();
// (B) ADD HTML CONTENT
$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);
// (C) SAVE TO DOCX ON SERVER
// $pw->save("convert.docx", "Word2007");
// (D) 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");
<?php
// (A) LOAD PHPWORD
require "vendor/autoload.php";
$pw = new \PhpOffice\PhpWord\PhpWord();
// (B) ADD HTML CONTENT
$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);
// (C) PHPWORD SUPPORTS MANY OTHER FORMATS!
$writers = [
"Word2007" => "docx",
"ODText" => "odt",
"RTF" => "rtf",
"HTML" => "html"
];
foreach ($writers as $format => $extension) {
$target = "output.$extension";
$pw->save($target, $format);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment