Author: Ashraf Gheith
Last month viewers: 1,652
Categories: PHP Tutorials
Read this article to learn how to quickly create Microsoft Word DOCX format using HTML templates.
Beginning with Office 2007, the default format for saving Word documents is .DOCX. Unfortunately, we are all used to using DOC files, but since then we needed to start converting to a new format which is not compatible with the old ways of programming.
Fortunately there are simple solutions like the VS PHP Word HTML by Raskin Veniamin that lets you easily create DOCX Word document dynamically from HTML.
Introduction to the Word DOCX Format for using PHP HTML to Word DOC Conversion
DOCX is "Office Open XML Document", is a zipped, XML-based file format developed by Microsoft for representing word processing documents.
The old (.doc) format was a proprietary and mostly undocumented format, while the .docx format is an open standard. That allows more content control with PHP without having Microsoft Office installed on the server.
PHP capabilities for XML manipulation are powerful, and DOCX is actually a ZIP archive with XML files inside. So just unzip your DOCX files and start doing magic.
Also a new thing we can do with PHP besides reading DOCX files, is writing them file easily. With this article you will learn how to create a DOCX files with PHP in several ways.
Since the launch of DOCX we have several solutions to use with PHP to create DOCX files, Here are some of them.
PHPWord package is a library that lets you create DOCX documents dynamically using your PHP 5.3+ scripts. One handicap of that library is that you need to learn how to build the structure of DOCX documents, it is not like HTML code.
DOMDocument, XSLTProcessor and ZipArchive
Another way is to do it all yourself with PHP and a DOCX template. You will need to create XML and XSLT files then load them with DOMDocument library.
Then process the styles with XSLTProcessor extension (which needs to be enabled in PHP). Transform it to OpenXML Word processing format, and then copy it in the template and zip it. The problem here is you need to create a template and you need to have a very good knowledge in PHP5 and XSLT.
Another solution is using a service like PHPDocx. It is a basically a library that calls a commercial API. One problem with it and that is it is not free. So you always need to pay. They have several packages that you can subscribe to and they have a good support.
VS PHP Word HTML class
It is open source and easy to use. This article will focus on this library as it is the easiest to use.
With Vs PHP Word class it is very simple and straight forward to generate DOCX documents. You just include VsWord library and create a HTML parser object. Then pass to the parser whatever HTML code you want to added to the document and save it a DOCX file.
You can also use another way by using nodes where you get more control in styling and manipulating your text. You also can create add page breaks or add templates and styles. You can even use WYSIWYG HTML editotr like TinyMCE and save the result as DOCX file.
A Simple Example
<?php require_once '../vsword/VsWord.php'; VsWord::autoLoad(); $doc = new VsWord(); $parser = new HtmlParser($doc); $parser->parse( '<h1>Hello world!</h1>' ); $parser->parse( '<h3>Hello world!</h3>' ); $parser->parse( '<p>Hello world!</p>' ); $parser->parse( '<h2>Header table</h2> <table> <tr><td>Coll 1</td><td>Coll 2</td></tr> </table>' ); $parser->parse( $html ); echo '<pre>'.($doc->getDocument() -> getBody() -> look()).'</pre>'; $doc->saveAs( 'htmlparser.docx' ); ?>
As you may have noticed, generating DOCX documents could not be simpler than using the solution provided by the VS PHP Word HTML. You just reuse the components and methods that you already use to generate HTML pages, and pass the resulting HTML to the great package.
To use this package you can download it as a ZIP file or install it with PHP composer with the instructions on the package page download section.
In the next part of this article you will learn how to create more complex documents with this class.
Meanwhile, if you liked this article or have questions, post a comment here.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
9. Showing error when trying to open the word file created - hari (2015-10-21 22:18)
Showing error when trying to open the word file created... - 1 reply
Read the whole comment and replies