You'd like to export a HTML document to a .doc / .docx MS Word file using simple HTML (+PHP or other server-side language)? Sounds quite complicated but is even simpler than PDF export! Learn how it works...
Drupal specific example
In our specific case we had the requirement to export a Drupal View to a .doc file for further offline processing and print.
We decided to use views_data_export.module for that, which is even handy in Drupal to export single content (simply return one result) or sets of results. This documentation explains how to configure a views data export: https://www.drupal.org/node/1820452
The module already provides a .doc export for views which simply sends the following headers
Content-Type: application/msword
Content-disposition: attachment; filename=myfile.doc
and provides HTML template files for the export header, body and footer. We'll make use of them (modify them) later in this blog post.
Anyway I ran into the problem that the exported documents were not configured as I required it. For example I missed:
- Paper size settings
- Paper format / orientation settings
- Paper margin settings
- Font family settings
- etc...
As it seems I'm not the only developer looking for solutions in views_data_export.module as 1959640.do shows (where I posted references to related issues).
I'll also post a patch for views_data_export.module there to set these styles as default.
So I started searching for general solutions and especially ways to set these settings via HTML / CSS. And YES, there are good solutions:
General HTML to Word doc know-how
I finally found the following interesting sources describing options for page layout and page sections which you should read to understand how HTML to Word export is working:
- http://sebsauvage.net/wiki/doku.php?id=word_document_generation
- http://www.codeproject.com/Articles/7341/Dynamically-generate-a-MS-Word…
- http://stackoverflow.com/questions/13340216/html-generated-microsoft-wo…
- http://stackoverflow.com/questions/8232982/change-margins-on-html-expor…
- http://stackoverflow.com/questions/22208694/create-word-doc-from-html-w…
- http://www.codeproject.com/Articles/32907/HTML-to-WordML
Finally I ended up writing my own views-data-export-doc-header.tpl.php-file in my custom template which is attached to this blog post.
It contains the following style settings in the header section to set the document page style as required:
in the HTML <head> section.
Furthermore I set the font-family, font-size, etc. in the body css:
body {
font-family: Arial, Verdana, Helvetica, Courier, sans-serif;
font-size:10pt;
}
... and YES it works! The pages layout is set as deserved and everything works fine. Try it yourself and simply export HTML documents to Word .doc with our without Drupal / PHP or other languages.
If this blog entry helped you, please leave a comment :)