Bharanikumar
Bharanikumar

Reputation: 25733

PHP Read and Write in MS WORD

I want to read/write word documents using PHP,

I know there is a function in php to work with COM objects which we can use to read and write word documents, but COM objects are only available on Windows and not on Linux.

My server is running linux and clients are uploading their docs/excel sheets to this server.

Is there some other way to read ms-excel/ms-word files using php?

Upvotes: 2

Views: 7666

Answers (5)

adilbo
adilbo

Reputation: 970

Create / Write a MS RTF Word Document by using this pure PHP Code

Use this free Lib: https://github.com/phprtflite/PHPRtfLite

Works on Windows, Linux and Mac OS

Take a look at the Documentation over here: http://sigma-scripts.de/phprtflite/docs/

Also read this if you what to now more about Office File Formats:

https://joelonsoftware.com/2008/02/19/why-are-the-microsoft-office-file-formats-so-complicated-and-some-workarounds/

<?php
/* ------------------------------------------------------------- */  
/* [Create MS Word RTF] Text to Microsoft Word #php #class #word */
/* ------------------------------------------------------------- */  

// SET HEADLINE & CONTENT
$headline = 'A Microsoft Word File created with PHP'; // H1-H6 TAGS WILL NOT WORK
$content = '
<bullet> Text <br><bullet> Text <br><bullet> Text <br><br>
<u>This is Lorem Ipsum Dolore Text</u>.
<strong>This is Lorem Ipsum Dolore Text</strong>.
<em>This is Lorem Ipsum Dolore Text</em>.
<b>This is Lorem Ipsum Dolore Text</b>.
<i>This is just another paragraph</i>. ';
// TAGS THAT WILL WORK: <bullet>, <br>, <strong>, <b>, <i>, <em> and <u>

// SET VAR
$dir = dirname(__FILE__);

// PHPRtfLite
require_once $dir . '/PHPRtfLite/lib/PHPRtfLite.php';
PHPRtfLite::registerAutoloader();
$rtf = new PHPRtfLite();
$sect = $rtf->addSection();

// ADD CONTENT AS HTML
// 'Arial' 'Verdana' 'Tahoma' Times New Roman' etc.
// TEXT_ALIGN_LEFT, TEXT_ALIGN_RIGHT, TEXT_ALIGN_CENTER, TEXT_ALIGN_JUSTIFY
$sect->writeText(
  $headline, 
  new PHPRtfLite_Font(24, 'Arial'), 
  new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_CENTER)
);                                                 
$sect->writeText(
  $content, 
  new PHPRtfLite_Font(12, 'Arial'), 
  new PHPRtfLite_ParFormat(PHPRtfLite_ParFormat::TEXT_ALIGN_LEFT)
);

// SAVE RTF DOCUMENT
$rtf->save($dir . '/' . basename(__FILE__, '.php') . '.rtf');

Upvotes: 0

Skrol29
Skrol29

Reputation: 5552

OpenTBS can read and modify DOCX (and other OpenXML files) documents in PHP using the technique of templates.

No temporary files needed, no command lines, all in PHP.

It can add or delete pictures. The created document can be produced as an HTML download, a file saved on the server, or as binary content in PHP.

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

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

Upvotes: 0

JamesHalsall
JamesHalsall

Reputation: 13475

It's worth noting that Microsoft advises against the automation of Office documents via COM objects:

Microsoft does not currently recommend, and does not support, Automation of Microsoft Office applications from any unattended, non-interactive client application or component (including ASP, ASP.NET, DCOM, and NT Services), because Office may exhibit unstable behavior and/or deadlock when Office is run in this environment.

Although you can create .docx files without COM objects however because of their XML foundations (you can use PHPDOCX for this). An added advantage with this method is that you don't need to have a local copy of Word installed (for .docx files) and you can also use it on a Linux server (in theory, although I'm not sure the PHPDOCX product supports that).

Upvotes: 2

Joseph Le Brech
Joseph Le Brech

Reputation: 6653

Sounds like a bad idea, as the best way to create and read word docs would be on a windows server.

For php you would like to try antiword to extract the text from word97/2000 files.

Upvotes: 1

prodigitalson
prodigitalson

Reputation: 60413

Check out phpexcel and phpword.

Upvotes: 2

Related Questions