PHP Create Word Document

Post June 27th, 2006, 9:34 pm

Dear All,

I want to create report in Word Document with PHP and database MySQL.
Anyone have example to write Word Document Using PHP?

Thansk


Rinto H
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post June 27th, 2006, 9:34 pm

  • Tannu4u
  • Proficient
  • Proficient
  • User avatar
  • Joined: Apr 29, 2004
  • Posts: 455
  • Loc: India
  • Status: Offline

Post June 27th, 2006, 9:58 pm

Here is the simplest method to write to a word file:

PHP Code: [ Download ] [ Select ]
<?
 
   $fp = fopen("amit.doc", 'w+');
 
   $str = "<B>This is the text for the word file created through php programming</B>";
 
 
 
   fwrite($fp, $str);
 
 
 
   fclose($fp);
 
?>
  1. <?
  2.  
  3.    $fp = fopen("amit.doc", 'w+');
  4.  
  5.    $str = "<B>This is the text for the word file created through php programming</B>";
  6.  
  7.  
  8.  
  9.    fwrite($fp, $str);
  10.  
  11.  
  12.  
  13.    fclose($fp);
  14.  
  15. ?>




if the above code is not enough then go to

http://www.programmershelp.co.uk/phpcreateword.php
Amit
My Blog http://www.amityadav.name

Post June 28th, 2006, 2:01 am

I have PHP create excel file like this:
<?php
$word = new COM("word.application") or die ("couldnt create an instance of word");
echo "loaded , word version{$word->version}";
//bring word to the front
$word->visible = 1;
//open a word document
$word->Documents->Add();
//add some text to the document
$word->Selection->TypeText("this is some sample text in the document");
//save the document as sampleword.doc
$word->Documents[1]->SaveAs("sampleword.doc");
//close word
$word->Quit();
//free object resources
$word->Release();
$word = null;
?>


But i want to format the content of Text. I want to change align, valign, and etc.
How i do this?

Thanks

Rinto H
  • gisele
  • Expert
  • Expert
  • User avatar
  • Joined: Nov 11, 2004
  • Posts: 579
  • Loc: Nimes (France)
  • Status: Offline

Post June 28th, 2006, 4:15 am

Hi,

Tannu4u was right, you can create .doc files just with fopen() , naming it file_name.doc and putting in some html source and it works.
To me it's far easier than using COM libraries.

You can also create Excel file or any open office doc file just writing in some html souce and giving the right extention.

You could also change the current document into a word file or a Excel file, no need to fopen and fputs try this simple test script to understand what I mean and check what does it look like :
PHP Code: [ Download ] [ Select ]
 
header("Content-Type: application/vnd.ms-excel");
 
print "<table border=\"1\"><tr><td><b>field1</b></td><td><b>field2</b></td></tr>";
 
print "<tr><td>value1 </td><td bgcolor=\"#137799\">value2 in blue cell bakground</td></tr></table>";
 
 
  1.  
  2. header("Content-Type: application/vnd.ms-excel");
  3.  
  4. print "<table border=\"1\"><tr><td><b>field1</b></td><td><b>field2</b></td></tr>";
  5.  
  6. print "<tr><td>value1 </td><td bgcolor=\"#137799\">value2 in blue cell bakground</td></tr></table>";
  7.  
  8.  


of course if you do a word file set the content-type to "application/msword" in header().
And do what you want for the page setting with html code (bgcolor, tables, align , etc.)
  • bluephoenix66
  • Born
  • Born
  • No Avatar
  • Joined: Aug 31, 2006
  • Posts: 2
  • Loc: London
  • Status: Offline

Post August 31st, 2006, 1:30 am

I have an e-commerce website and I am trying to have a webpage create a .doc or .rtf document to print labels based on the customers' addresses in a mysql database.

For this reason, I need to create a document based on a labels template.

I have saved the labels template as whslabels.rtf on the website, and on every label I have written "Address1", "Address2", "Address3", ... then I have the mentioned webpage do a str_replace replacing the "Address..." words with the customer's address, and then save this to a new file named "newlabels6.rtf".

$template_file = "whslabels.rtf";
$handle = fopen($template_file , "r");
$contents = fread($handle, filesize($template_file));

$original= array("Address1", "Address2");
$new = array("pizza", "beer");

$newphrase = str_replace($original, $new, $contents);

$handle2 = fopen("newlabel6.rtf" , "w");

fwrite ( $handle2 ,$newcontents);
fclose ($handle);
fclose($handle2);

but when I go to open the new .rtf file it doesn't seem to able to open it.

What am I doing wrong?
  • bluephoenix66
  • Born
  • Born
  • No Avatar
  • Joined: Aug 31, 2006
  • Posts: 2
  • Loc: London
  • Status: Offline

Post August 31st, 2006, 1:33 am

the exact error I get is "The document file or path is not valid...."
  • azhaz_ri
  • Born
  • Born
  • No Avatar
  • Joined: Nov 02, 2006
  • Posts: 1
  • Loc: Malaysia
  • Status: Offline

Post November 2nd, 2006, 7:39 pm

try put this code at in your code..
header("Content-Type: application/vnd.ms-word");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("content-disposition: attachment;filename=test.doc");
  • studentpk
  • Born
  • Born
  • No Avatar
  • Joined: Jan 19, 2007
  • Posts: 4
  • Status: Offline

Post January 19th, 2007, 2:30 pm

Hi
i need some help
i want to edit a word file using php
means i want to search a word and replace it with another using php.
looking forword for ur replies
  • Truce
  • Guru
  • Guru
  • No Avatar
  • Joined: Apr 25, 2004
  • Posts: 1478
  • Loc: Washington DC
  • Status: Offline

Post January 23rd, 2007, 10:57 pm

Please don't bring up really old threads, especially if they aren't the same issue you're having.

To answer your question, http://www.php.net/str_replace

You really should read up on PHP if you're going to be using it.
  • studentpk
  • Born
  • Born
  • No Avatar
  • Joined: Jan 19, 2007
  • Posts: 4
  • Status: Offline

Post January 24th, 2007, 2:16 pm

Sorry dear.
i did use str_replace

check out the code


<?PHP
$filename = "c:/PHP.doc";

$f="c:/d.doc";
$fp = fopen($f, 'wb');
$fp_temp = fopen($filename, 'rb');


$old_contents = fread ( $fp_temp, filesize ($filename));
fclose ( $fp_temp);
echo $old_contents;
$constants=str_replace("PHP","PHP1",$old_contents);
fwrite($fp, $constants);
fclose($fp);
?>

but by this file d.doc did not open in word and that is the main problem at my end.
please help
  • Truce
  • Guru
  • Guru
  • No Avatar
  • Joined: Apr 25, 2004
  • Posts: 1478
  • Loc: Washington DC
  • Status: Offline

Post January 25th, 2007, 10:20 pm

Well, what is happening when you try to open the resulting file in Word? Is PHP.doc a valid Word document? Have you attempted to compare the two documents (PHP.doc and d.doc) in a Hex editor to see what changes where made?
  • studentpk
  • Born
  • Born
  • No Avatar
  • Joined: Jan 19, 2007
  • Posts: 4
  • Status: Offline

Post January 27th, 2007, 2:17 pm

it simply did not opem and shows an error.

well i did not use and editor to compare
but PHP.doc shows size of 24kb but d.doc shows 25kbs
  • Truce
  • Guru
  • Guru
  • No Avatar
  • Joined: Apr 25, 2004
  • Posts: 1478
  • Loc: Washington DC
  • Status: Offline

Post January 27th, 2007, 2:25 pm

Well, if you really want us to help you are going to need to give us some more info here. Unfortunately, we can't read minds, and it's illegal for me to hack into your computer, regardless of what my intent is.

So tell us what the error is. Post both of the files for us to review (like, upload them via FTP in binary mode and give us links).
  • studentpk
  • Born
  • Born
  • No Avatar
  • Joined: Jan 19, 2007
  • Posts: 4
  • Status: Offline

Post February 7th, 2007, 2:06 pm

Hi

i reach the problem.
well when i try to replace a word with equal number of charchters. it works fine. but when i try to replace with bigger word it didnt open in the word.

means if replace PHP with HPP it will work
but if try to replace PHP with PHP1 it will not.

this might help you to solve the problem

thanks.
  • gregcaulder
  • Born
  • Born
  • No Avatar
  • Joined: Mar 23, 2007
  • Posts: 3
  • Status: Offline

Post March 23rd, 2007, 9:43 pm

rinto_harianja wrote:
I have PHP create excel file like this:
<?php
$word = new COM("word.application") or die ("couldnt create an instance of word");
echo "loaded , word version{$word->version}";
//bring word to the front
$word->visible = 1;
//open a word document
$word->Documents->Add();
//add some text to the document
$word->Selection->TypeText("this is some sample text in the document");
//save the document as sampleword.doc
$word->Documents[1]->SaveAs("sampleword.doc");
//close word
$word->Quit();
//free object resources
$word->Release();
$word = null;
?>


But i want to format the content of Text. I want to change align, valign, and etc.
How i do this?

Thanks

Rinto H


Ok i have code exactley like this but for creating a word document. The problem im having is that I get a fat error on this line "$word->Selection->TypeText("this is some sample text in the document");"

Any suggestions. It seems that I cant add anything to the document.

I have a similar script to open a template, but it ALWAYS says that it cant find the blasted file, not matter what I try ot use for the location of the file.

Can anyone help me with that at all?

Greg
  • Anonymous
  • Bot
  • No Avatar
  • Joined: 25 Feb 2008
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post March 23rd, 2007, 9:43 pm

Post Information

  • Total Posts in this topic: 23 posts
  • Users browsing this forum: No registered users and 222 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© Unmelted Enterprises 1998-2009. Driven by phpBB © 2001-2009 phpBB Group.