Welcome to a quick tutorial on how to convert HTML to DOCX using PHP. Need to generate formal Word reports? Capture a webpage into a document? Yes, that can actually be done pretty easily.
In order to convert HTML to DOCX in PHP, we have to download and use a third-party library. PHPWord is a good choice, and we can use Composer to get the latest version – composer require phpoffice/phpword
. Thereafter, a short code snippet to generate DOCX from HTML:
require "vendor/autoload.php";
$pw = new \PhpOffice\PhpWord\PhpWord();
$section = $pw->addSection();
\PhpOffice\PhpWord\Shared\Html::addHtml($section, "<span>FOO</span>", false, false);
$pw->save("HTML.docx", "Word2007");
That’s the gist of it, but let us walk through more detailed examples in this guide – Read on!
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
CONVERT HTML TO DOCX IN PHP
All right, let us now get into the example of how to convert HTML into a DOCX document in PHP.
STEP 1) DOWNLOAD PHPWORD
- PHP is unable to generate DOCX documents natively. To do that, we are going to use an open-source library called PHPWord.
- The easiest way to get it is to use a package manager called Composer. A small hassle, but Composer does offer quite a bit of convenience for a ton of libraries.
- After you have installed Composer, navigate to your project folder in the command prompt (or terminal).
- Run
composer require phpoffice/phpword
, and watch it will automatically get the latest version into thevendor/
folder. - Check out the official PHPWord documentation for more code examples and references.
STEP 2) PHP CONVERT HTML TO DOCX
<?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");
The script to generate the DOCX documents is ridiculously simple.
- (A) Start by including the PHPWord library, and creating the object.
- (B) Add a new section to the DOCX document, then simply insert your HTML code.
- (C & D) Output the file, and that’s it!
EXTRA) PHPWORD SUPPORTS OTHER FILE FORMATS
<?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);
}
If you poke around the sample scripts that PHPWord provides in \vendor\phpoffice\phpword\samples
, you will notice that it is actually capable of more than just .DOCX
– It can also create rich text files, open document text, and even PDFs. So be sure to make full use of it.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SUPPORT
600+ free tutorials & projects on Code Boxx and still growing. I insist on not turning Code Boxx into a "paid scripts and courses" business, so every little bit of support helps.
Buy Me A Meal Code Boxx eBooks
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA BITS & LINKS
Yep, that is all the tutorial, and here are some extra bits and links that may be useful to you.
LINKS & REFERENCES
- Need to convert HTML to PDF as well? Check out my other guide.
- Need to output in Excel? Check out this guide.
TUTORIAL VIDEO
INFOGRAPHIC CHEAT SHEET

THE END
Thank you for reading, and we have come to the end of this short guide. I hope that it has helped you with your project, and if there is stuff that you wish to add to this guide, please feel free to comment below. Good luck with your future projects and happy coding!