Easy way to make Word documents with images from PHP
Note: This article was originally published at Planet PHP on 17 January 2011.Article originally from rooJSolutions blog
A It'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 A myfile.html
Unfortunately, althoughA looking at abiwords source code, it should have A worked, 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 xmlA generatorA for the content (good old Flexy templates). then just run them through abiword as planned.
abiword --to=doc A myfile.abw
Quick, easy and pretty Word documents generated from web pages..