Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Dynamic Word document generation

    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?

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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:
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
    <?mso-application progid="Word.Document"?>
    <w:wordDocument <!-- SEVERAL namespaces will be here, rather not type them atm -->>
      <o:DocumentProperties>
        <!-- Contains info like the title, author, etc
      </o:DocumentProperties>
      <w:fonts>
        <!-- Contains font information -- I've yet to establish a need for this at least for extracting for web use -->
      </w:fonts>
      <w:lists>
        <!-- Lists information -- Establishing what this does exactly, defines how the lists are displayed -->
      </w:lists>
      <w:styles>
        <!-- Contains all style information similar to how a css works -- looks different of course -->
      </w:styles>
      <w:divs> 
        <!-- Divisions?  I've yet to link individual ids to location within the word document -->
      </w:divs>
      <w:body>
        <wx:sect>
          <!-- 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 -->
          <w:p>
            <w:pPr>
              <!-- Paragraph properties, borders, bgcolors, alignment -->
            </w:pPr>
            <w:rPr>
              <!-- Default paragraph run properties, fontfamily, color, bold, italic etc. -->
            </w:rPr>
            <w:r>
                <w:rPr>
                 <!-- Current run properties -->
                </w:rPr>
                <w:t>Look I'm Text :P</w:t>
            </w:r>
          </w:p>
        </wx:sect>
      </w:body>
    </w:wordDocument>
    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
    Last edited by Fou-Lu; 06-21-2005 at 04:01 PM.

  • #3
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.
    Last edited by Fou-Lu; 06-21-2005 at 04:23 PM.

  • #5
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I think it so isn't worth it.

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I agree. And to my knowledge there is unfortunatly no other way to do this, sorry to say.

  • #7
    Regular Coder
    Join Date
    May 2005
    Posts
    563
    Thanks
    0
    Thanked 3 Times in 3 Posts
    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.

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •