2 Steps To Convert HTML To DOCX In PHP (Simple Example)

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!

 

 

TABLE OF CONTENTS

 

DOWNLOAD & NOTES

Here is the download link to the example code, so you don’t have to copy-paste everything.

 

EXAMPLE CODE DOWNLOAD

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.

 

SORRY FOR THE ADS...

But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.

Buy Me A Coffee Code Boxx eBooks

 

 

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.

 

TUTORIAL VIDEO

 

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 the vendor/ folder.
  • Check out the official PHPWord documentation for more code examples and references.

 

STEP 2) PHP CONVERT HTML TO DOCX

2-convert.php
<?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

3-more.php
<?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.

 

 

EXTRAS

Yep, that is all the tutorial, and here are some extra bits and links that may be useful to you.

 

LINKS & REFERENCES

 

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!

2 thoughts on “2 Steps To Convert HTML To DOCX In PHP (Simple Example)”

  1. Hello! How do I save images to ms word from html?
    The code below not working for image tag
    $html = “HELLO WORLD!”;
    $html .= “This is a paragraph of random text”;
    $html .= “A tableCell”;
    $html .= “”;

Comments are closed.