views:

18943

answers:

9

Whats the available solutions for PHP to create word document in linux environment?

+2  A: 

If you really need to create a Word document, then your best bet is probably to use COM to generate the Word document through Microsoft Word. See http://www.programmershelp.co.uk/phpcreateword.php and http://drewd.com/2007/01/25/reading-from-a-word-document-with-com-in-php for some examples.

Otherwise my advice would be to use RTF or HTML documents - see http://conort.googlepages.com/generate-word-from-php and and http://paggard.com/projects/rtf.generator/ for example.

Jay
+7  A: 

Add header and output HTML as you usually do.

header("Content-type: application/vnd.ms-word");
header("Content-Disposition: attachment;Filename=document_name.doc");

echo "<html>";
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=Windows-1252\">";
echo "<body>";
echo "<b>My first document</b>";
echo "</body>";
echo "</html>";

Make sure you don't use external stylesheets. Everything should be in the same file.

If you need to produce "real" Word documents you need a Windows-based web server and COM automation. I highly recommend Joel's article on this subject.

Sergey Kornilov
Problem with this approach is that I can't embed graph in my document.
If you need to produce "real" Word documents you need a Windows-based web server. I highly recommend Joel's article on this subject: http://www.joelonsoftware.com/items/2008/02/19.html
Sergey Kornilov
A: 

A bit of a pickle here because you can't utilize the COM library nor use fopen to just create a word file. However, you can try creating a PDF file first and then using a PDF-word converter such as abiword to convert your PDF file to doc. That's not the best way to do it, but I hope it helps...

Also check out how the doc format works

Swati
A: 

There are 2 options to create quality word documents. Use COM to communicate with word (this requires a windows php server at least). Use openoffice and it's API to create and save documents in word format.

DreamWerx
+2  A: 

The Apache project has a library called POI which can be used to generate MS Office files. It is a Java library but the advantage is that it can run on Linux with no trouble. This library has its limitations but it may do the job for you, and it's probably simpler to use than trying to run Word.

Another option would be OpenOffice but I can't exactly recommend it since I've never used it.

Mr. Shiny and New
+6  A: 

OpenOffice templates + OOo command line interface.

  1. Create manually an ODT template with placeholders, like [%value-to-replace%]
  2. When instantiating the template with real data in PHP, unzip the template ODT (it's a zipped XML), and run against the XML the textual replace of the placeholders with the actual values.
  3. Zip the ODT back
  4. Run the conversion ODT -> DOC via OpenOffice command line interface.

There are tools and libraries available to ease each of those steps.

May be that helps.

Ivan Krechetov
+2  A: 

By far the easiest way to create DOC files on Linux, using PHP is with the Zend Framework component phpLiveDocx.

From the project web site:

"phpLiveDocx allows developers to generate documents by combining structured data from PHP with a template, created in a word processor. The resulting document can be saved as a PDF, DOCX, DOC or RTF file. The concept is the same as with mail-merge."

It is completely free to download and use. For more information, please take a look at:

http://www.phplivedocx.org/articles/brief-introduction-to-phplivedocx/

Seems to require signing up to a 3rd party service for API access that does the heavy lifting.
garrow
A: 

You may also use a free LGPL library: phpdocx.

You may create pretty spphisticated docx documents with a few lines of code.

Eduardo
+1  A: 

Visible = 1; //open an empty document $word->Documents->Add(); //do some weird stuff $word->Selection->TypeText($sFileContent); $word->Documents[1]->SaveAs($ROOT."/".$sFileName.".doc"); //closing word $word->Quit(); //free the object $word = null; return $sFileName; } ?>

"; $Return .="Row[0]"; $Return .="Row[1]"; $sReturn .=""; fWriteFile("test",$Return,$PATH_ROOT); ?>

hoang