View Full Version : Dynamic Word document generation
06-21-2005, 06:45 AM
Is it possible to generate a Word document dynamically using a template? The server is a Windows Server.
If so... How would I go about doing it?
06-21-2005, 04:58 PM
The only way I've found to correctly do this, (assuming you want to populate data, but do not have an actual word document), is to create an xml document.
This is a *****. Plain and simple, and I really hope that there is an extension to do this. But on the plus side, I'd be there would be tons of data that can be stripped out of a wordML document and still be displayed correctly.
Now to create word as an xml, you have to think of each line as a new paragraph, and each section of different formatted text as a span. Got that? Ok good, nice and simple. So you will end up with like a million <p> tags, and thats actually how word stores them as well, with a namespace for the worddocument of w. Most word elements are prefixed with w, some are others though.
Now, styling and formatting works slightly different in word. Depends entirly on what you want and what you have. If you use a css based template, you can create a styles section of your document, otherwise you can create inline. Both work fine and to my knowledge one will work without the other.
Images can be done as well, store data as base64 encoded mime to a w:binData tag. I'll give an example of what the wordML will look like after.
All text is displayed in wordML as a <w:t> element, presented as a child to a <w:r> element - your run element - which is also your span element (if formatted differently).
Now, I've only used xml from the one direction, not from the other. But I figure putting it together will be almost as simple.
Here is the basic structure of a wordML document:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<w:wordDocument <!-- SEVERAL namespaces will be here, rather not type them atm -->>
<!-- Contains info like the title, author, etc
<!-- Contains font information -- I've yet to establish a need for this at least for extracting for web use -->
<!-- Lists information -- Establishing what this does exactly, defines how the lists are displayed -->
<!-- Contains all style information similar to how a css works -- looks different of course -->
<!-- Divisions? I've yet to link individual ids to location within the word document -->
<!-- Sometimes I have p show up here, I've yet to determine why its embeded here sometimes, or if it really matters for that matter -->
<!-- Paragraph properties, borders, bgcolors, alignment -->
<!-- Default paragraph run properties, fontfamily, color, bold, italic etc. -->
<!-- Current run properties -->
<w:t>Look I'm Text :P</w:t>
Worth it? Dunno about that. I hope theres an easier way to create a word document from a template, but something says I doubt it. Downside to this way of creating a document, is that the xml can only be display as a word document in Word 2003+. Anything less, your out of luck. We'll see if you have any luck from other posters, otherwise, I can help you through together a script to create this for you, so long as your willing to only have specific individuals download it :P
06-21-2005, 05:02 PM
1 problem, the document is fairly formatted with lots of tables, it's 9 pages and has 300 odd fields going into it.
Is there a way to get rid of margins so word can just read it as html but have no margins?
06-21-2005, 05:20 PM
Formatting would not be an issue in the least. Anything you can do in a word document you can place in your xml. It would be awhile to identify everything we would need to do that with. The downside is, I can help you create it, but I cannot test it as I do not have word2003 on my computer to work with.
I'm a little concerned about the size, but at only 9 pages, I'd estimate a file size with tables and lists to be ~200kb in size. Probably trimable with maybe a couple of small images as well.
Oh, look at my example there as well. Thats a bare bones example of wordML missing tons of stuff that would be included. All that creates is a word doucument with that tiny sentence in it.
Keep that in mine as well, XML will drive the size up by about 30% from the original worddocument size. Of course, it can be resaved as a worddocument.
06-22-2005, 03:49 AM
I think it so isn't worth it.
06-22-2005, 05:42 AM
I agree. And to my knowledge there is unfortunatly no other way to do this, sorry to say.
06-22-2005, 05:46 AM
I thought there was a way to open an application on the server and create something in that and return it. Similar to how you connect to a mssql database stored in a local folder.
06-22-2005, 06:07 AM
hmm, shell_exec you could probably use to run and load the word application.
Downside is, this would assume you are on a windows server with word installed.
May be worth looking into though, save you tons of time if you can get around the wordML IMO.
Powered by vBulletin® Version 4.2.2 Copyright © 2015 vBulletin Solutions, Inc. All rights reserved.