Page 1 of 1

Creating a Word document with PHP

Posted: Tue Apr 06, 2004 9:51 am
by callaght
Dear All,

I’ve recently created a PHP application that opens a newly created Microsoft Word document as an rtf file i.e. 991149.rtf is created in an existing directory on the server (Microsoft Windows 2000 using IIS)

It’s been an experience! But I’m left with three problems that I can’t find the answer for no matter how hard I’ve tried and searched over the last few days, anyways…

The first, and most important, problem is when a new document is opened on the client machine (XP Pro)…

On the Word title bar it will say ‘991149.rtf (Read Only)

How do I make it read and write?

The code that creates the file on the server looks something like this:-

Code: Select all

$fp = fopen( $szFile, "w+b" ); 
			
fwrite( $fp, $rtf );
fclose( $fp );
The next problem is that when the document is opened, the cursor in Word is always at the 1st character on the 1st line. I want it to appear on the last character of the last line, how do I do this? I know it may be a Word question, but I thought I’d ask while I’m at it.

Lastly, when I click on the hyperlink that starts the creating a Word document off, it opens another blank web page (which has run the PHP code to create the word document).

When I click on 'Open' in the download dialogue box - because it has downloaded the rtf file from the server, this new blank web page still hangs around. How can I kill it after I’ve clicked on open download?

Your help on any or all of these questions would be greatly appreciated.

Many thanks in advance and for you patience in reading this.

Cheers,
Tom

Posted: Tue Apr 06, 2004 11:48 am
by TheBentinel.com
Since RTF only describes the text, not the last known viewing position within it, I doubt you can write an RTF that will pull up at position other than character 1. If your webserver is a Windows machine, can you interface with Word's API's to create a real Word document? Then you might be able to set the position.

On the read-only bit, what user creates the file? (The user running the webserver process, presumably) What permissions does that file get written out with? Maybe you can change those permissions when you create the file. (Not sure how, but maybe the FileSystem object has something about it?)

You might be able to fire off a window.close() in the blank window (in HTML) to make it go away. Or you might be able to avoid creating the window altogether if you use an image for the PHP script. When the user clicks the link to build the file, fire off some javascript to create a new image with a URL of your PHP script. That way the script gets executed, but there's no window to worry about.

Posted: Tue Apr 06, 2004 12:04 pm
by callaght
Hi Dave,

Thanks for your reply.

Looks from what you say about RTF that I`m stuck with the cursor being at position 1 line 1. I`m going to have to stick with rtf now as people are already using it and it took me long enough to get it working that way.

The user running the webprocess is creating the file. When I boot it up I log onto it as 'Administrator'. How do I find out the permissions the file gets written out with?


Tried using the window.close() here:-

Code: Select all

print "<SCRIPT language=javascript>  ";
print "	window.opener.frames.transactionwindow.location.reload(); ";
print "	document.location='documents/$filename'; ";
print "</SCRIPT>  ";
print "<SCRIPT language=javascript>  ";
print "	window.close(); "; 
print "</SCRIPT>  ";
print "</HEAD> "; 
print "</HTML> ";
But the window still hangs around.

And I`ve tried using window.close(); here:-

Code: Select all

print "<SCRIPT language=javascript>  ";
print "	window.opener.frames.transactionwindow.location.reload(); ";
print "	document.location='documents/$filename'; ";
print "	window.close(); "; 
print "</SCRIPT>  ";		
print "</HEAD> "; 
print "</HTML> ";

But the window closes before I get the dialogue box asking me to 'Save' or 'Open' the file. Am I using window.close() the wrong way?

Cheers,
Tom

Posted: Tue Apr 06, 2004 1:04 pm
by TheBentinel.com
So the PHP file creates a document, then redirects the user to it using javascript. Hmmm...

How about a timer? Use setTimeout to wait 5 seconds after the window is displayed to close it.

Code: Select all

print "<SCRIPT language=javascript>  "; 
print "   setTimeout('window.close();',5000); "; 
print "   window.opener.frames.transactionwindow.location.reload(); "; 
print "   document.location='documents/$filename'; "; 
print "</SCRIPT>  ";       
print "</HEAD> "; 
print "</HTML> ";
That might wait 5 seconds, then close the window, while still allowing Word to load and display the file. The user may get prompted "The window is trying to close, allow it?", I never know when the prompt will come up and when it won't. Worth a try at least.