PHP Classes

PHP Word Document Generator to Convert HTML to DOCX Part 1: Using VS PHP Word HTML class or PHPDocX Example

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

Author:

Viewers: 5,736

Last month viewers: 1,652

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.




Loaded Article

Contents

Using a PHP Word Document Generator Solution

Introduction to the Word DOCX Format  for using PHP HTML to Word DOC Conversion

How to Create a Doc File using PHP to Convert HTML to DOCX

Using VS PHP Word HTML class to Generate Word Document from HTML Dynamically

Download the PHP to Word Document from HTML Package


Using a PHP Word Document Generator Solution

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.

Docx and PHP

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.

How to Create a Doc File using PHP to Convert HTML to DOCX

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.

VS PHP Word HTML class

Vs PHP Word HTML 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.

Using VS PHP Word HTML class to Generate Word Document from HTML Dynamically

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

?>

Download the PHP to Word Document from HTML Package

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:



Comments:

15. güzel - Bozyazi isg (2023-03-23 03:31)
harika... - 0 replies
Read the whole comment and replies

14. how to auto download the word file - Agustu Atihuta (2020-08-25 21:52)
-... - 0 replies
Read the whole comment and replies

13. No word file :( - Francisco Javier (2018-11-24 03:22)
only getting a xml tree... - 1 reply
Read the whole comment and replies

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



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog PHP Word Document Gen...   Post a comment Post a comment   See comments See comments (28)   Trackbacks (0)