PHP Classes
elePHPant
Icontem

Create Microsoft Word DOCX files from HTML in PHP Part 1: Simple Example

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Create Microsoft Word...   Post a comment Post a comment   See comments See comments (24)   Trackbacks (0)  

Author: Ashraf Gheith

Posted on:

Categories: PHP Tutorials

Many PHP applications require to export documents in Microsoft Word formats. However, most PHP developers only have experience in generating HTML Web pages.

Read this article to learn how to quickly create Microsoft Word DOCX format using HTML templates.




Contents

Introduction

Introduction to the DOCX Format

Some Solutions to Create DOCX using PHP

VsWord Class for Creating DOCX Word Document Dynamically from HTML

Conclusion


Introduction

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 need to start converting to a new format which is not compatible with the old ways of programming.

Fortunately there are simple solutions like the VsWord by Raskin Veniamin that lets you easily create DOCX Word document dynamically from HTML.

Docx and PHP

Introduction to the DOCX Format

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.

Some Solutions to Create DOCX using PHP

Since the launch of DOCX we have several solutions to use with PHP to create DOCX files, Here are some of them.

PHPWord

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.

PHPDocx

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.

VsWord

VsWord is a package created by Raskin Veniamin which allows you to dynamically create files DOCX simply by converting HTML to .DOCX. It won the PHP Innovation Award edition of January 2015.

It is open source and easy to use. This article will focus on this library as it is the easiest to use.

VsWord Class for Creating DOCX Word Document Dynamically from HTML

With VsWord 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' );

?>

Conclusion

As you may have noticed, generating DOCX documents could not be simpler than using the solution provided by the VSWord class. You just reuse the components and methods that you already use to generate HTML pages, and pass the resulting HTML to the great package.

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

1,402,234 PHP developers registered to the PHP Classes site.
Be One of Us!

Login Immediately with your account on:

FacebookGmail
HotmailStackOverflow
GitHubYahoo


Comments:

12. apprication - AYEDOUN D Fiacre (2016-08-03 14:52)
how can use... - 1 reply
Read the whole comment and replies

11. error - AYEDOUN D Fiacre (2016-07-17 19:46)
how can use... - 0 replies
Read the whole comment and replies

10. error in VsWord.php - Ian Onvlee (2015-10-28 21:23)
error in VsWord.php... - 1 reply
Read the whole comment and replies

4. Licenses - marcel haldemann (2015-10-26 18:36)
GPL = evil... - 2 replies
Read the whole comment and replies

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

8. docx templates - Grega Sever (2015-10-04 20:38)
filling existing docx templates... - 1 reply
Read the whole comment and replies

5. Not Getting the docx file - Tapas Kumar Samal (2015-10-02 01:35)
How to get the generated file... - 2 replies
Read the whole comment and replies

7. Fervado - fenando vazquez (2015-10-02 01:34)
Muy Interresante y practico... - 0 replies
Read the whole comment and replies

2. Nice tut - TAIWO JOHN Ogunyemi (2015-10-01 18:15)
Nice work... - 1 reply
Read the whole comment and replies

6. Complements - Alofe Oluwafemi (2015-10-01 18:14)
Complements... - 1 reply
Read the whole comment and replies



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog Create Microsoft Word...   Post a comment Post a comment   See comments See comments (24)   Trackbacks (0)