Recent blog posts
Generating Word Files with WordML (xml) as a Template
I've been spending the past day messing around with WordML to get it to generate Word files from PHP. The goal was to create printable tables. As George Bush said as the Iraq War slid toward failure: "Mission Accomplished."
My strategy was to start with a Word file, save it as xml, aka WordML, and then using the file as an xml template for the table layout. This is analogous to designing your output forms in NVU or Dreamweaver, and then turning it into a template.
Note that I saved the document out as an .xml file, not an .htm file. The .htm file could also be used as a template, but it doesn't retain all the Word features. There's also an alternate format, .htx, that is a MIME encoded .htm with enclosures, but the entire thing is MIME encoded, and decoding (and re-encoding) is a pain. XML is clean and relatively simple, and allows you to embed graphics.
Step by Step
1. To convert a document into a template, first, save it out as xml.
2. Then, use HTMLTidy to reformat the xml into something readable. Use the command "tidy -i -xml filename > outputfile". That will indent the xml code so you can edit it.
3. Cut and paste the XML into your logic, just like you might use HTML. Use the <? and ?> style, not 'echo', please. There is a LOT of XML.
4. You must set the content type to application/msword. This will tell the browser to open the file with Word. In PHP, above the template, add a header line:
So, in PHP, you might have this block at the top of the page, in addition to your own code:
That's all there is to it.