NogDog;10880771 wrote:The problem is that MS Word files are not flat text files; they are in a proprietary format of their own. You could write the text into a rich text format (.rtf) file, or if using the latest version of Office the new XML format supported by Word, but you still need to know the protocol for either of those formats (just like you do to create HTML or XML files).
Some of you more seasoned PHP/MySQL veterans may laugh at this, but I thought Iād share since it worked for me. š
I have a PHP/MySQL program that allows users to enter data and then export the information to a Word file. I finally figured out how to export all the data to a Word file where I can control the headers, footers, Table of Contents, page numbers, etc.!
Word XML has too many variables and Iām sure some of you are familiar with all the garbled XML thatās mind-boggling to look at. So, I created an RTF (thanks to NogDog mentioning trying RTF) template file called RTF.rtf with the header, footer, Table of Contents, page numbers, etc. RTF doesnāt save macros, but thatās not a big deal. Maybe Iāll find something out later.
I have a cover page, TOC, then section headers with Wordās Heading 1 (and Heading 2 where needed) set for each section. I modified them to look exactly like I wanted (colors, borders, etc.) After creating the sections, I updated my TOC. Under each section, I simply used Wordās INCLUDETEXT function:
{ INCLUDETEXT "http://websiteaddress.com/file_for_this_section.php?id=IDIDIDIDID" \c HTML }
The IDIDIDIDID is just a generic name I gave as a placeholder. Use whatever you want. I saved the RTF.rtf file to the same folder as my code and did the following:
$filename = "./RTF.rtf/"; // ./ for the current directory, in case some were wondering
$handle = fopen($filename, "r+"); // r+ = Open for reading and writing; place the file pointer at the beginning of the file. (ala php.net)
$contents = fread($handle, filesize($filename)); // it was in the example, but I donāt even know if I need this line
$edit = preg_replace('/IDIDIDIDID/', $_GET['id'], $contents); // read the entire RTF.rtf file and replace the IDIDIDIDID with the id number you are trying to call up
You cannot (at least to my limited knowledge⦠and I got sick of looking for how to do this) perform a āfind and replaceā (i.e. preg_replace) on the XML file because of the Word binData (UUENCODED/Binary) information generated with each change of the XML file. You simply break the document and get error codes on the lines with the binData information.
Youāll see references to $row_reports, but that was for calling up the information to get the report title, etc. After the connection script and getting the report information, I used thisā¦
header('Content-Type:application/msword');
header('Content-Disposition: attachment; filename="'.$row_reports['title'].'.doc"'); // in order to place the actual report title from the database into the filename.doc that youāll see when you click the link and get the Open/Save/Cancel box. This code may be off since I removed the str_replace info I had in here for my own use, but you get the idea.
header("Pragma: no-cache");
header("Expires: 0");
Then I pasted the newly edited code that contains the actual id number vs. the IDIDIDIDID.
echo $edit;
I click the link for āprint.php?id=25ā or whatever id it is and I get the prompt to Open/Save/Cancel my Word document. All the formatting is there and I simply update the TOC.
Just FYIā¦
I havenāt run into the problem with exporting the Word doc, but when I was doing earlier testing before generating the Word doc, I sometimes had to clear my cache.
I hope this works for you like it's working for me and I'd LOVE to hear how to improve this, cuz I'm sure it could be done. š