PHP Create Word Document

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

Post March 26th, 2007, 2:22 am

I have gotten my code above to work correctly. No problems their anymore. What i am having a problem with is being able to insert an image from a URL into the file itself. how would i go about doing that?


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

Post March 26th, 2007, 2:22 am

  • Denis Slaveckij
  • Born
  • Born
  • No Avatar
  • Joined: Jul 07, 2007
  • Posts: 1
  • Status: Offline

Post July 7th, 2007, 12:40 pm

I think it's better use rtf format. Look at <a href="http://www.phprtf.com">http://www.phprtf.com</a>.
  • masterblue
  • Born
  • Born
  • No Avatar
  • Joined: Apr 16, 2008
  • Posts: 1
  • Status: Offline

Post April 16th, 2008, 2:26 am

Hi, I tried the code below to make a word document.

Code: [ Select ]
$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;
  1. $word = new COM("word.application") or die ("couldnt create an instance of word");
  2. echo "loaded , word version{$word->version}";
  3. //bring word to the front
  4. $word->visible = 1;
  5. //open a word document
  6. $word->Documents->Add();
  7. //add some text to the document
  8. $word->Selection->TypeText("this is some sample text in the document");
  9. //save the document as sampleword.doc
  10. $word->Documents[1]->SaveAs("sampleword.doc");
  11. //close word
  12. $word->Quit();
  13. //free object resources
  14. $word->Release ();
  15. $word = null;


What I get is an error :
-----------------------------------------------------------------
Fatal error: Uncaught exception 'com_exception' with message 'Error [0x80020003] Kan lid niet vinden. ' in D:\xampp\htdocs\basiccv\Maakpdf.php:42 Stack trace: #0 D:\xampp\htdocs\basiccv\Maakpdf.php(42): com->Release() #1 {main} thrown in D:\xampp\htdocs\basiccv\Maakpdf.php on line 42
-----------------------------------------------------------------

I Checked the documentation and I saw that the Release function is for PHP 5.X
I Use PHP Version 5.2.3
So the version is right.

Is RELEASE the line were you have to put the directory in?

Can someone help me with an understanable sollution?

Thanks!
  • anirban
  • Born
  • Born
  • No Avatar
  • Joined: Jun 09, 2008
  • Posts: 1
  • Status: Offline

Post June 9th, 2008, 12:09 pm

Hi ,

When I am running the code:

<?php
$word = new COM("word.application");

$word->Visible = 0;
$word->Documents->Add();
$word->Selection->PageSetup->LeftMargin = '2"';
$word->Selection->PageSetup->RightMargin = '2"';

//Setup the font
$word->Selection->Font->Name = 'Verdana';
$word->Selection->Font->Size = 8;

//Write some text
$word->Selection->TypeText("This is a test document");
//Save the document as DOC file
$word->Documents[1]->SaveAs("D:\\Domains\\jobgool.com\\wwwroot\\makeresume\\testdoc.doc");

//quit and release COM resources
$word->quit();
$word->Release();
$word = null;

?>


I am getting the following error:

Fatal error: Uncaught exception 'com_exception' with message 'Failed to create COM object `word.application': Invalid syntax ' in D:\Domains\xxxxxx.com\wwwroot\makeresume\testdoc.php:2 Stack trace: #0 D:\Domains\xxxxxx.com\wwwroot\makeresume\testdoc.php(2): com->com('word.applicatio...') #1 {main} thrown in D:\Domains\xxxxxx.com\wwwroot\makeresume\testdoc.php on line 2

Can someone provide me any suggestion of code modification?

Thanks in advance
Anirban.
  • sejpal_dipesh
  • Born
  • Born
  • No Avatar
  • Joined: Jun 21, 2008
  • Posts: 1
  • Status: Offline

Post June 23rd, 2008, 9:31 am

hi

I am using the following code
<?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 m getting following error while running this code on the server

Fatal error: Class 'COM' not found in file.php on line 24

Please help me as soon as possible

Thank you
  • D1N0
  • Newbie
  • Newbie
  • No Avatar
  • Joined: Sep 26, 2008
  • Posts: 5
  • Status: Offline

Post September 26th, 2008, 1:14 pm

So using the methods described so far i have been able to create my word document. Thank you. So much easier. But now it doesn't close the file. It puts the next echo statement that i am using to test the results of the form data that will be going into the word document. I don't know where i am going wrong?
here is the code:
Code: [ Select ]
<?php
//-----This Sections is all of the data for the request email----
 
//Get Data from the Form
 
$from = $_POST['email'];
$company = $_POST['company'];
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phone = $_POST['phonenumber'];
$software = $_POST['program'];
$version = $_POST['version'];
$sr = $_POST['sr'];
$filename = sprintf("2-%10s.doc",$sr);
$srnumber = sprintf ("2-%10s", $sr);
 
$string =
 
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=$filename");
 
echo (" Fist Name: $firstname
    Last Name: $lastname
    Company:   $company
    Phone:     $phone
    From:      $from
    Software:  $software
    Version:   $version
    SR Number: $srnumber
    ");
 
$echo_string = "
    Fist Name: &nbsp $firstname<br />
    Last Name: &nbsp $lastname<br />
    Company: &nbsp $company<br />
    Phone:&nbsp $phone<br />
    From: &nbsp $from <br />
    Software: &nbsp $software<br />
    Version: &nbsp $version<br />
    Request: &nbsp $srnumber<br />
    File Name: &nbsp $filename<br />";
 
echo $echo_string;
  1. <?php
  2. //-----This Sections is all of the data for the request email----
  3.  
  4. //Get Data from the Form
  5.  
  6. $from = $_POST['email'];
  7. $company = $_POST['company'];
  8. $firstname = $_POST['firstname'];
  9. $lastname = $_POST['lastname'];
  10. $phone = $_POST['phonenumber'];
  11. $software = $_POST['program'];
  12. $version = $_POST['version'];
  13. $sr = $_POST['sr'];
  14. $filename = sprintf("2-%10s.doc",$sr);
  15. $srnumber = sprintf ("2-%10s", $sr);
  16.  
  17. $string =
  18.  
  19. header("Content-Type: application/vnd.ms-word");
  20. header("Expires: 0");
  21. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  22. header("content-disposition: attachment;filename=$filename");
  23.  
  24. echo (" Fist Name: $firstname
  25.     Last Name: $lastname
  26.     Company:   $company
  27.     Phone:     $phone
  28.     From:      $from
  29.     Software:  $software
  30.     Version:   $version
  31.     SR Number: $srnumber
  32.     ");
  33.  
  34. $echo_string = "
  35.     Fist Name: &nbsp $firstname<br />
  36.     Last Name: &nbsp $lastname<br />
  37.     Company: &nbsp $company<br />
  38.     Phone:&nbsp $phone<br />
  39.     From: &nbsp $from <br />
  40.     Software: &nbsp $software<br />
  41.     Version: &nbsp $version<br />
  42.     Request: &nbsp $srnumber<br />
  43.     File Name: &nbsp $filename<br />";
  44.  
  45. echo $echo_string;


It takes the formdata and uses it to create the filename, and the contents, but it adds the $echo_string to the document as well. Any ideas?
  • thumrith
  • Born
  • Born
  • No Avatar
  • Joined: Jan 16, 2009
  • Posts: 1
  • Status: Offline

Post January 24th, 2009, 7:33 am

PHP Code: [ Select ]
header("Content-Type: application/vnd.mspowerpoint");
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. header("Content-Type: application/vnd.mspowerpoint");
  2. print "<table border=\"1\"><tr><td><b>field1</b></td><td><b>field2</b></td></tr>";
  3. print "<tr><td>value1 </td><td bgcolor=\"#137799\">value2 in blue cell bakground</td></tr></table>";
  4.  


HOW CAN I CREATE A NEW SLIDE IN THE POWER POINT USING THIS CODE ???
  • Pado
  • Born
  • Born
  • No Avatar
  • Joined: Apr 02, 2009
  • Posts: 1
  • Status: Offline

Post April 2nd, 2009, 10:30 am

Hey, thanks for the help. This was very helpful. I was quickly able to create a word document with PHP. I used the following code:
PHP Code: [ Select ]
 
    $fp = fopen("Passes.doc", 'w+');
    foreach($pass_arry as $pass_arry)
    {
        $pass_arry = $pass_arry."\n";
        fwrite($fp, $pass_arry);
    }
    fclose($fp);
 
  1.  
  2.     $fp = fopen("Passes.doc", 'w+');
  3.     foreach($pass_arry as $pass_arry)
  4.     {
  5.         $pass_arry = $pass_arry."\n";
  6.         fwrite($fp, $pass_arry);
  7.     }
  8.     fclose($fp);
  9.  

It worked great, but I have a slight problem. When word opens up, I get another window that says "File Conversion-Passes1.doc. Select the encoding that makes your document readable. Text coding: Windows(default) blah blah" and I have to press "okay" for it to open. Is there anyway I can make it so this window doesn't pop up and the Word document opens immediately? Thanks!
  • purya
  • Born
  • Born
  • No Avatar
  • Joined: Jul 22, 2010
  • Posts: 1
  • Status: Offline

Post July 22nd, 2010, 1:00 am

Hello
i reach the problem
I don't know how do i change the page size of microsoft word(like A4,A5 ,...) with php?
  • Vinita
  • Born
  • Born
  • No Avatar
  • Joined: Oct 06, 2010
  • Posts: 1
  • Status: Offline

Post October 6th, 2010, 12:33 am

any1 please tel how to use images on the ord document generated using php
  • Almaron
  • Born
  • Born
  • No Avatar
  • Joined: Oct 26, 2010
  • Posts: 1
  • Status: Offline

Post October 26th, 2010, 2:43 am

Hi.
Got another quiz.
What I have:
1. .doc file named "parse.doc" with placeholders like [+key+]
2. an array called $tmp like this $tmp[key]=value
3. a name for a new file $fname;

What I need to do:
1. Take the contents of parse.doc and replace placeholders with values from the array and then put the text into a new file called $fname.doc

Tried to parse it using str_replace and preg_replace (POSIX regular), but had no luck. Does anybody have suggestions?
P.S. COM does not work. It's CentOS server.

P.P.S. Problem solved using ZipArchieve class to parse the docx document.
  • walkerstone
  • Born
  • Born
  • No Avatar
  • Joined: Aug 28, 2012
  • Posts: 1
  • Status: Offline

Post August 28th, 2012, 8:51 am

Sorry to ressurect a very dead thread. Just adding this in the hope it helps anyone that has a problem like this in future as this returned high in the rankings.

An easy way to generate word documents without going to the lengths of COM libraries is to use the FILE_PUT_CONTENTS.

Here's a little function I use to spew out a simple replace on my template. It's fairly limited in use but can be improved upon. A working base at least:

Code: [ Select ]
function generateDocsFromTemplate( $templatePath, $newFileName, $stringToReplace, $replacementString ){

    $template = fopen($templatePath, "r");
    $contents = fread($template, filesize($templatePath));

    // This block takes care of matching the length mismatch
    // Always make sure the replacement code in the template
    // is longer than the new string.
    $clientCodeLength = strlen($clientCode);
    $replacementLength = strlen($stringToReplace);
    $difference = $replacementLength - $clientCodeLength;

    for( $i=0; $i<$difference; $i++ ) {
        $clientCode .= chr(0);
    }

    $newcont = str_replace( $stringToReplace, $replacementString, $contents );

    $outputFileName = $newFileName . '.doc'; // Extension can be (.docx)

    file_put_contents( $outputFileName, $newcont);

    fclose( $template );

}
  1. function generateDocsFromTemplate( $templatePath, $newFileName, $stringToReplace, $replacementString ){
  2.     $template = fopen($templatePath, "r");
  3.     $contents = fread($template, filesize($templatePath));
  4.     // This block takes care of matching the length mismatch
  5.     // Always make sure the replacement code in the template
  6.     // is longer than the new string.
  7.     $clientCodeLength = strlen($clientCode);
  8.     $replacementLength = strlen($stringToReplace);
  9.     $difference = $replacementLength - $clientCodeLength;
  10.     for( $i=0; $i<$difference; $i++ ) {
  11.         $clientCode .= chr(0);
  12.     }
  13.     $newcont = str_replace( $stringToReplace, $replacementString, $contents );
  14.     $outputFileName = $newFileName . '.doc'; // Extension can be (.docx)
  15.     file_put_contents( $outputFileName, $newcont);
  16.     fclose( $template );
  17. }

Post Information

  • Total Posts in this topic: 27 posts
  • Users browsing this forum: No registered users and 146 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
 
 

© 2011 Unmelted, LLC. Ozzu® is a registered trademark of Unmelted, LLC.