When developing online projects, it is often necessary to save the HTML page as a document that can be used separately, for example, sent by e-mail, viewed and edited offline. A convenient way is to export the HTML page to one of the most commonly used text formats – doc.
The free open-source PHP-module “html_to_doc” can be used to export the HTML page to the DOC document. It can convert the HTML to the DOC document, which will be correctly processed by the text editor MS Word. If the HTML page includes images, they will be embedded in the DOC document.
The PHP module can be downloaded from https://github.com/Korchy/html_to_doc
Installation:
Download the ExportToWord.inc.php file and include it to your project.
The ExportToWord.inc.php file contains all the code. Just place it in your project directory. You do not need to install anything else.
For proper functionality you need to edit the following parameters in the module:
- $imgRef – specify a relative (from the project root) path to the directory with images included in the HTML-code.
- $imgDir – specify the full (absolute) path to the directory with the images.
Using:
With the require or include directive add the module ExportToWord.inc.php to your project code.
To export the HTML-code to the DOC document call the ExportToWord::htmlToDoc() function from the module, passing in the parameters the HTML code itself, the CSS style sheet and the file name with the path where you want to get the exported DOC document.
1 2 3 4 5 6 |
require_once(dirname(dirname(__FILE__)).'/ExportToWord.inc.php'); $html = '<html><body><div class = "test">Test</div></body></html>'; $css = '<style type = "text/css">.test {font-weight: 600;}</style>'; $fileName = 'c:/test.doc'; ExportToWord::htmlToDoc($html, $css, $fileName); |
After the function executing the DOC-document containing the HTML-code will be generated in the specified place.