Page 1 of 1

Help with PHP and MSWORD docs

Posted: Sat May 13, 2006 11:27 pm
by lsnyder
I wrote a script that replaces "template vars" in a word document with values a user submits from a form. When the submit button is hit, the template MSWORD document is opened up and read into variable. The "template vars" are then replaced with the form values and the document is saved and the user may then download the document. But when you attempt to open up the document, you get an error that says :


"Word experienced an error trying to open this file. Try these suggestions. *Check the file permissions for the document or drive *make sure there is sufficient free memory and diskspace * open the file with the text recovery converter"

When I comment out the loop that replaces the "template vars" so no changes to the template file are made, and php saves the file, you can open it and view it regularly, so i know the problem is something with the str_replace, preg_replace, ereg_replace functions i've tried. All get the same error when they're used and you try to open the documents.

What do i need to do so that the word document is viewable?

Someone please help! If you want to view the files, you can download them in zip format here:

http://www.lancesnyder.com/problem.zip

If someone could please help me i would REALLY appreciate it!!

Posted: Sun May 14, 2006 1:41 am
by Burrito
what happens when you echo the 'modified' string before 'injecting' it into word?

Posted: Sun May 14, 2006 3:25 pm
by Ollie Saunders
Make sure the functions you are using to make the replacements are 'binary-safe' if they are not word will believe the file to be corrupt.

Of course Word may well create checksums etc that prevent you from doing such a thing.

Posted: Sun May 14, 2006 6:50 pm
by lsnyder
When I echo the 'modified' string, it prints the 'special characters' word uses and the text from the document. I can see that the string contains the values from the form, when it's saved though it's not able to be opened.

What functions would be binary safe?

Posted: Sun May 14, 2006 6:54 pm
by lsnyder
Here is the part of the script that replaces the values in the form

Code: Select all

$template_vars = array(
					  "{LOCATION_NAME}" => $values['location_name'],
					  "{LOCATION_ADDR}" => $values['address'],
					  "{CITY_STATE_ZIP}" => $values['city'].", ".$values['state']." ".$values['zipcode'],
					  "{PHONE}" => $values['phone_areacode']."-".$values['phone1']."-".$values['phone2'],
					  "{FAX}" => $values['fax_areacode']."-".$values['fax1']."-".$values['fax2'],
					  "{NUM_ROOMS}" => $values['number_rooms'],
					  "{PRICE_PER_ROOM}" => $values['price_room'],
					  "{QUARTERLY_PAYMENT}" => $values['quarterly_payment']);

	//open template
	$location = "/var/www/vhosts/myactivesupport.com/htdocs/service_contracts/support_agreement_template.doc";
	$file = fopen($location,"rb");
	$data = fread($file,filesize($location));
	fclose($file);

	//replace template vars with values
	foreach ($template_vars as $template_var => $value) {
		$data = str_replace($template_var, $value, $data);
	}

Posted: Sun May 14, 2006 7:41 pm
by Christopher
str_replace() is binary safe, so it probably is not that. You could test if is a checksum problem by replacing your tags with strings of the same length (e.g. "{LOCATION_NAME}" => '123456789012345' ) and see if you still have the problem.

Posted: Sun May 14, 2006 8:06 pm
by lsnyder
I solved the problem by converting the documents to RTF. Thanks everyone for your effort in trying to help me solve this problem though!!

Posted: Mon May 15, 2006 2:12 am
by Ollie Saunders
I solved the problem by converting the documents to RTF. Thanks everyone for your effort in trying to help me solve this problem though!!
Good idea
What functions would be binary safe?
The manual will always tell you