I'm currently finishing up a web site project that involves a rather extensive set of backend accounting tools. One of these tools manages monthly royalty payments, and in order to make things as easy as possible on our staff, the site is designed to automatically generate printable envelopes with each recipient's mailing address, as well as our return address. Also necessary is the generation of perhaps a single .DOC file wherein each royatly statement is generated on a separate page boundary so they can be immediately printed.

Technically, this isn't a PHP-exclusive question, but rather, I'm curious as to what everyone thinks the best approach would be here. As far as I know there's no .DOC-generation API, so what would you recommend I do for the royalty statements? Furthermore, I'm wondering if anyone has any experience with having to generate printable envelope templates from a browser (although this would also be a likely application of the .DOC format, assuming I can find a simple enough way to generate them from PHP).

Any help/advice/perspective would be greatly appreciated. Thanks!

    The Microsoft Word file format is documented here:
    http://www.aozw65.dsl.pipex.com/generator_wword8.htm

    I think you need to write your PHP script so it outputs very simple comma-delimited text files and then in Microsoft Word, create mail merge templates to read the text files and do all that.

    I think it would be a nightmare to, in PHP, try to do Microsoft Word's job.

      You will NEVER make .docs, but you can make a pretty good equivalent, RTF.

      Best thing to do is to save a Word doc with the formats you want as an RTF file.

      Then search through the RTF code to find the stuff you want to change programmatically, and write the PHP to change only that part.

      This should get you started. If you need more help, let me know. It's mostly boring, but not that hard if you don't doze off.

        5 months later

        An easy way to create .DOC files (i.e MS-Word "Documents") is to create the Word doc as a file whose content is HTML and whose extension is ".doc". (To get a template, save a word doc as "website", which gives it an .html extension. If you then open the file in an ordinary editor you will see the HTML/XML-Code of the Word doc).

        If you doubleclick on the file, Word will open it as a word doc.

        If you give it an extension other than ".doc", you can open it only from within MS-Word.

        This is what I have already tried and it works.

        The following is just an idea at the moment. If it works, it can be used to create word docs in web servers running php:

        Create the word doc with html content, tell the browser its mime type is an msword document (sorry, I don't know the exact mime type) and send out the html content. Maybe IE (and NS?) can be motivated to open the page as a Word doc then.

        Martin

          The '.doc' extension identifies the file as one made by Word.

          Sticking a .doc extension on an HTML file does NOT make it a Word doc.

          Word has a translator for HTML files, but look out:

          I don't know about later versions of word but Word 6 does not contain an HTML translator in its standard install, and a .doc extension on an HTML file would result in gibberish being displayed.

          Also other programs can read Word-format .doc files, and these programs would fail to read an mislabelled HTML file.

          Further, the formatting needed to print envelopes using HTML seems hardly worth the trouble.

            Is there any chance that you could use pdf files? I don't know why you couldn't if you don't need to edit the information after it is spit out.

            We do something similar with our statements at the end of the month, we generate an html page with all of the data we need for the statement, then it goes through HTMLDOC (www.easysw.com/htmldoc)

            We have it in a loop, so that all of the cities involoved in our project get a statement, and it is all generated into 1 pdf file.

            It shouldn't be hard to do the envelopes either, I'm pretty sure HTMLDOC has a setting for paper size, and I think you can even embed a specific tray to print from into the pdf. Neither of these are something I would take a chance on if I were sending the pdf to the general public, but if it is just something you are printing in your office, and mailing out it should work well.

            I'm not a big server guy, so I wouldn't be much help compiling HTMLDOC on your system, but if you have any programming questions about it let me know (jcantrell@mygov.us), I have found a lot of great uses for it in the past few months.

            JAKE

              Just use free pdf a free pdf class for php.
              Works well for me.
              www.fpdf.org look at the example tutorials

                I tried using fpdf before I found HTMLDOC, (which is free also). It actually worked very well for me, but it meant that I had to reprogram everything I had already made into an html format with the fpdf code. I was in a time crunch, so I found htmldoc, all you do is run a passthru command (It is actually made to run command line) and it spits out an html page as a pdf.

                Go here and put in any webpage, and it will output it to a pdf.
                http://www.easysw.com/htmldoc/pdf-o-matic.php

                JAKE

                  AMV, You have inspired me to run envelopes for our statements from our site, here is the code I came up with, hope it helps.

                  ///////////////////////////////
                  //FILE 1 autopdf_mail.php
                  <?php

                  $filename = "$siteurl/create_mail.php";
                  $options = "--left 0 --right 0 --top 0 --bottom 5";

                  Write the content type to the client...

                  header("Content-Type: application/pdf");
                  header("Content-Disposition: attachment; filename=\"$month - Invoices.pdf\"");
                  flush();

                  			# Run HTMLDOC to provide the PDF file to the user...
                  			passthru("htmldoc -t pdf --jpeg --webpage $options '$filename'");

                  ?>

                  //////////////////////////////
                  //FILE 2 create_mail.php

                  <?php
                  print "<!-- MEDIA LANDSCAPE YES -->";
                  print "<!-- MEDIA SIZE 4.125x9.5in -->";
                  print "<!-- FOOTER RIGHT \"\" -->";
                  include("./snippets/priceformat.php");
                  include("./includes/common.php");

                  $getcities =mysql_query("SELECT * FROM tbl_City");
                  while ($a_row =mysql_fetch_array($getcities))
                  {
                  $citynum = $a_row[id];
                  $city = strtoupper($a_row[city]);

                  			print "<Table align=center height=65%>";
                  			print "<tr><td valign=bottom><img src=\"./images/spacer.gif\" width=120></td><td valign=bottom>";
                  			print "	CITY OF $city<BR>
                  					Attention: $a_row[fcfname] $a_row[fclname]<BR>
                  					$a_row[fcmailaddress]<BR>
                  					$a_row[fcmailcity], $a_row[fcmailstate] $a_row[fcmailzip]";
                  			print "</td></tr></table>";
                  
                  
                  					print "<!-- PAGE BREAK -->";
                  
                  
                  	}			

                  ?>

                  The HTML comments are acutally statements to the HTMLDOC program, they set the paper orientation, the paper size (our envelope size is there now), and the blank footer right just takes the pdf page numbers out. Also the Page break comment at the bottom makes it so each time it goes through the loop, it starts on a blank page, so each city gets printed on a seperate evelope.

                  I'm here if you have questions.

                  JAKE

                    7 months later

                    Hi,

                    while using HTMLDoc i noticed that my font sizes are completly incorrect. Does HTMLDoc ignor <font>-Tags?

                    Thanx for help.

                      Write a Reply...