PHPWord is a PHP library that let you read and write Microsoft Word document (and some additional file format like RTF, openXML and HTML) PHPWord is very useful to generate docx or read existing one in your web application. In this article, I’ll cover about setup and hello world tutorial. Then we’ll discuss about further application such as how to create docx from HTML!
Before we begin, you’ll need to install PHPWord via Composer (Composer is a Dependency Manager Tool for PHP. Like npm for Node.js) Add the below code to your composer.json file.
Then open the command prompt and type below to trigger the install.
composer install
Composer will pull PHPWord and all its dependencies to your project location. Now we’re ready to move forward with the code. For Example, here is the steps if you want to create a new docx document with “Hello Word” text.
First, load PHPWord using autoload.php located under vendor/autoload.php.
require_once 'vendor/autoload.php';
Then create a new PHPWord object. This will represent your document file.
$phpWord = new \PhpOffice\PhpWord\PhpWord();
The structure of the Word document are separated into sections. Text or element can’t be added directly to the document without section. So we’re going to add one.
$section = $phpWord->addSection();
Then create a Font object to custom a font style, Then assign the font object to the section.
$fontStyle = new \PhpOffice\PhpWord\Style\Font(); $fontStyle->setBold(true); $fontStyle->setName('Arial'); $fontStyle->setSize(16); $myTextElement = $section->addText('Hello World'); $myTextElement->setFontStyle($fontStyle);
And finally, output the document to the web browser which will popup a download/Save as dialog box.
Important note: The whole php file must not have any echo, print or any kind of display message or it will corrupt the generated docx file.
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('helloWorld.docx');
You and see tutorial video here for step by step detail.
Now that’s the basic. Another interesting example is to create a docx file from HTML.
Suppose that you already have piece of HTML that you want to put into docx file, you’ll need to convert that into HTML string with javascript, then pass it to PHPWord via form POST. Below is example code using jQuery.
.
Then in gendocx.php we’ll take incoming HTML string and put it into docx document like this.
require_once 'vendor/autoload.php'; $phpWord = new \PhpOffice\PhpWord\PhpWord(); $section = $phpWord->addSection(); \PhpOffice\PhpWord\Shared\Html::addHtml($section, $_POST['htmlstring']); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment;filename="test.docx"'); $objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007'); $objWriter->save('php://output');
Or See tutorial video below!
Written ByTK Engineer, Web Developer, Ex-Mutual Funds Software Developer. He founded Red Stapler in 2015 to share useful resources for everyone interested in web design, web development and programming. Subscribe his YouTube Channel or Twitter for weekly tips and tutorials.