Create a Word document using PHP on Linux

What are the available PHP solutions for creating a Word document in linux environment?

+58
linux php ms-word document
Sep 24 '08 at 1:52
source share
10 answers

real word documents

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

fake HTTP headers to cheat Word to open raw HTML

A fairly common (but unreliable) alternative:

 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>Fake word document</b>"; echo "</body>"; echo "</html>" 

Make sure you are not using external style sheets. Everything should be in one file.

Please note that this is not sending the actual Word document. It simply forces browsers to offer it as a download and by default use the .doc extension. Older versions of Word can often open this without a warning / security message and simply import raw HTML into Word. PHP submission sending this misleading Content-Type header is not a real file format conversion.

+32
Sep 24 '08 at 2:07
source share
โ€” -

PHPWord can create Word documents in docx format. It can also use an existing .docx file as a template - template variables can be added to the document in the format $ {varname}

It has an LGPL license, and the examples that came with the code worked well for me.

+27
Nov 08 2018-11-11T00:
source share

OpenOffice templates + OOo command line interface.

  • Manually create an ODT template with placeholders, for example [% value-to-replace%]
  • When creating an instance of a template with real data in PHP, unzip the ODT template (this is archived XML) and perform a text replacement of the placeholders against the actual values โ€‹โ€‹against the XML.
  • Zip ODT back
  • Run the ODT โ†’ DOC conversion through the OpenOffice command-line interface.

Tools and libraries are available to facilitate each of these steps.

Maybe this helps.

+20
Sep 25 '08 at 8:05
source share

The easiest way to create .doc files on Linux using PHP is with the Zend Framework component phpLiveDocx .

On the project website:

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

+10
May 14 '09 at 6:48 a.m.
source share

OpenTBS can create dynamic DOCX documents in PHP using template technology.

No temporary files required, no commands, all in PHP.

It can add or remove photos. The created document can be created as loading HTML, a file stored on the server, or as binary content in PHP.

It can also combine OpenDocument files (ODT, ODS, ODF, ...)

http://www.tinybutstrong.com/opentbs.php

+7
Mar 31 '11 at 10:40
source share

Following Ivan Krechetov, answer, here is the function that performs the merge (actually just replaces the text) for docx and odt, without the need for an additional library.

 function mailMerge($templateFile, $newFile, $row) { if (!copy($templateFile, $newFile)) // make a duplicate so we dont overwrite the template return false; // could not duplicate template $zip = new ZipArchive(); if ($zip->open($newFile, ZIPARCHIVE::CHECKCONS) !== TRUE) return false; // probably not a docx file $file = substr($templateFile, -4) == '.odt' ? 'content.xml' : 'word/document.xml'; $data = $zip->getFromName($file); foreach ($row as $key => $value) $data = str_replace($key, $value, $data); $zip->deleteName($file); $zip->addFromString($file, $data); $zip->close(); return true; } 

This will replace [Human Name] with Mina and [Human Name] with Mooo:

 $replacements = array('[Person Name]' => 'Mina', '[Person Last Name]' => 'Mooo'); $newFile = tempnam_sfx(sys_get_temp_dir(), '.dat'); $templateName = 'personinfo.docx'; if (mailMerge($templateName, $newFile, $replacements)) { header('Content-type: application/msword'); header('Content-Disposition: attachment; filename=' . $templateName); header('Accept-Ranges: bytes'); header('Content-Length: '. filesize($file)); readfile($newFile); unlink($newFile); } 

Remember that this function can damage the document if the replacement string is too general. Try using verbal replacement strings such as [Person Name].

+6
Nov 09 '10 at 21:37
source share

The Apache project has a library called POI that can be used to create MS Office files. This is a Java library, but the advantage is that it can run on Linux without a problem. This library has its limitations, but it can do the job for you, and it is probably easier to use than trying to run Word.

Another option would be OpenOffice, but I cannot recommend it for sure, since I have never used it.

+3
Sep 24 '08 at 14:38
source share
 <?php function fWriteFile($sFileName,$sFileContent="No Data",$ROOT) { $word = new COM("word.application") or die("Unable to instantiate Word"); //bring it to front $word->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; } ?> <?php $PATH_ROOT=dirname(__FILE__); $Return ="<table>"; $Return .="<tr><td>Row[0]</td></tr>"; $Return .="<tr><td>Row[1]</td></tr>"; $sReturn .="</table>"; fWriteFile("test",$Return,$PATH_ROOT); ?> 
+3
Jul 24 2018-10-24T00:
source share

There are two options for creating quality text documents. Use COM to communicate with the word (this requires at least a Windows php server). Use openoffice and its API to create and save documents in text format.

0
Sep 24 '08 at 7:33
source share

Take a look at the PHP COM docs (comments are helpful) http://us3.php.net/com

0
Nov 26 '10 at 15:45
source share



All Articles