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!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Notes HTML To DOCX Useful Bits & Links
Tutorial Video The End

 

 

DOWNLOAD & NOTES

First, here is the download link to the example source code as promised.

 

QUICK NOTES

  • A copy of PHPWord is not included in the zip file. Please download and get the latest version from their website.
  • Run 2-convert.php for the conversion demo.
If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

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 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.

 

 

USEFUL BITS & LINKS

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

 

LINKS & REFERENCES

 

TUTORIAL VIDEO

 

INFOGRAPHIC CHEAT SHEET

Convert HTML To DOCX in PHP (Click to enlarge)

 

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 some stuff that you wish to add to this guide, please feel free to comment below. Good luck with your future projects and happy coding!

Leave a Comment

Your email address will not be published.