Easy way to make Word documents with images from PHP
Note: This article was originally published at Planet PHP on 18 January 2011.Article originally from rooJSolutions blog
AIt's yet another "solve this quickly and cheaply" problems....
How to produce nice Word documents with images and all, without ending up doing lots of hard coded PHP calling some obtuse library in a way that would be difficult to maintain.
This one took a bit longer than expected, my first idea was to generate html files, then run them through abiword's command line, as that was the hints I got from googling.
abiword --to=doc Amyfile.html
Unfortunately, althoughAlooking at abiwords source code, it should have Aworked, nothing actually came out. After trying a few other magic tools like unoconv I was getting close to admitting defeat.
However, after a nice walk away from the desk and putting the thinking cap on, it came to me.. abw is abiwords xml file format. It's about as simple as html. So why not generate a abw file, and convert that into word.
Images in abw files are just base64 at the end of the file, each given a specific name, and reference in the body of the document where you want to use them. Trivial to generate... and even easier if you cheat a little by not using an dom based xmlAgeneratorAfor the content (good old Flexy templates). then just run them through abiword as planned.
abiword --to=doc Amyfile.abw
Quick, easy and pretty Word documents generated from web pages..